home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / utility / emxinfo.zip / INFO.C < prev    next >
Text File  |  1992-03-16  |  111KB  |  4,659 lines

  1. /* info -- a stand-alone Info program.
  2.  
  3.    Copyright (C) 1987, 1991 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Info.
  6.  
  7.    GNU Info is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY.  No author or distributor accepts
  9.    responsibility to anyone for the consequences of using it or for
  10.    whether it serves any particular purpose or works at all, unless he
  11.    says so in writing.  Refer to the GNU Emacs General Public License
  12.    for full details.
  13.  
  14.    Everyone is granted permission to copy, modify and redistribute
  15.    GNU Info, but only under the conditions described in the GNU Emacs
  16.    General Public License.   A copy of this license is supposed to
  17.    have been given to you along with GNU Emacs so you can know your
  18.    rights and responsibilities.  It should be in a file named COPYING.
  19.    Among other things, the copyright notice and this notice must be
  20.    preserved on all copies.  */
  21.  
  22. /* This is GNU Info:
  23.  
  24.    Version 1.42
  25.    Fri Feb  7 1992
  26. */
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <signal.h>
  33. #include <pwd.h>
  34. #include <errno.h>
  35. #include "keys.h"
  36. #if !defined (errno)
  37. extern int errno;
  38. #endif /* !errno */
  39. #include <ctype.h>
  40.  
  41. #include "getopt.h"
  42.  
  43. #if defined (USG)
  44. struct passwd *getpwnam ();
  45. #include <fcntl.h>
  46. #include <sys/termio.h>
  47. #include <string.h>
  48.  
  49. #  if defined (USGr3)
  50. #    include <sys/stream.h>
  51. #    include <sys/ptem.h>
  52. #    undef TIOCGETC
  53. #  endif /* USGr3 */
  54. #define bcopy(source, dest, count) memcpy(dest, source, count)
  55. char *index(s,c) char *s; { char *strchr(); return strchr(s,c); }
  56. char *rindex(s,c) char *s; { char *strrchr(); return strrchr(s,c); }
  57. #else /* !USG */
  58. #include <io.h>
  59. #include <sys/file.h>
  60. #include <sys/termio.h>
  61. #include <strings.h>
  62. #include <termcap.h>
  63. #endif /* USG */
  64.  
  65. #if !defined (DEFAULT_INFOPATH)
  66. #define DEFAULT_INFOPATH ".;/emx/info;/usr/gnu/info;/usr/local/emacs/info;/usr/local/lib/emacs/info"
  67. #endif /* DEFAULT_INFOPATH */
  68.  
  69. #define UNIX
  70.  
  71. typedef struct nodeinfo {
  72.   char *filename;
  73.   char *nodename;
  74.   int pagetop;
  75.   int nodetop;
  76.   struct nodeinfo *next;
  77. } NODEINFO;
  78.  
  79. typedef struct indirectinfo {
  80.   char *filename;
  81.   int first_byte;
  82. } INDIRECT_INFO;
  83.  
  84. typedef int Function ();
  85.  
  86. #define PROJECT_NAME "GNU Info"
  87.  
  88. #define barf(msg) fprintf(stderr, "%s\n", msg)
  89.  
  90. /* Some character stuff. */
  91. #define control_character_threshold 0x020 /* smaller than this is control */
  92. #define control_character_bit 0x40    /* 0x000000, must be off. */
  93.  
  94. #define info_separator_char '\037'
  95. #define start_of_node_string "\037"
  96.  
  97. #ifdef CTRL
  98. #undef CTRL
  99. #endif
  100.  
  101. #define CTRL(c) ((c) & (~control_character_bit))
  102.  
  103. #define UNCTRL(c) to_upper(((c)|control_character_bit))
  104.  
  105. #ifndef to_upper
  106. #define to_upper(c) (((c) < 'a' || (c) > 'z') ? c : c-32)
  107. #define to_lower(c) (((c) < 'A' || (c) > 'Z') ? c : c+32)
  108. #endif
  109.  
  110. #define CTRL_P(c) ((unsigned char) (c) < control_character_threshold)
  111.  
  112. #define NEWLINE '\n'
  113. #define RETURN CTRL('M')
  114. #define DELETE 0x07f
  115. #define TAB '\t'
  116. #define ABORT_CHAR CTRL('G')
  117. #define PAGE CTRL('L')
  118. #define SPACE 0x020
  119. #define ESC CTRL('[')
  120. #define control_display_prefix '^'
  121.  
  122. #define TAG_TABLE_END_STRING "\037\nEND TAG TABLE"
  123. #define TAG_TABLE_BEG_STRING "\nTAG TABLE:\n"
  124. #define NODE_ID "Node:"
  125. #define NNODENAME 4        /* Default amount to grow nodename list by. */
  126. #define FILENAME_LEN 256
  127. #define NODENAME_LEN 256
  128. #define STRING_SIZE 256
  129. #define nodeend_sequence "\n\037"
  130.  
  131. #define whitespace(c) ((c) == ' ' || (c) == '\t')
  132. #define cr_whitespace(c) ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r')
  133.  
  134. /* Reverse Video */
  135. char *terminal_inverse_begin;    /* {tc_char,"mr" */
  136. char *terminal_end_attributes;    /* {tc_char,"me" */
  137.  
  138. /* All right, some windows stuff. */
  139.  
  140. typedef struct {
  141.   /* Absolute x and y coordinates for usable portion of this window. */
  142.   int left, top, right, bottom;
  143.   /* Absolute cursor position in this window. */
  144.   int ch, cv;
  145. } WINDOW;
  146.  
  147. typedef struct _wind_list {
  148.   int left, top, right, bottom;
  149.   int ch, cv;
  150.   struct _wind_list *next_window;
  151. } WINDOW_LIST;
  152.  
  153. WINDOW the_window = {0, 0, 80, 24, 0, 0};
  154. WINDOW_LIST *window_stack = (WINDOW_LIST *)NULL;
  155. WINDOW terminal_window = {0, 0, 80, 24, 0, 0};
  156.  
  157. /* Not really extern, but defined later in this file. */
  158. extern WINDOW echo_area;
  159. void *xmalloc (), *xrealloc ();
  160. char *getenv (), *next_info_file (), *opsys_filename ();
  161. int build_menu (), find_menu_node ();
  162. void swap_filestack (), pop_filestack ();
  163.  
  164. /* A crock, this should be done in a different way. */
  165. #define MAX_INDIRECT_FILES 100 
  166.  
  167. /* The info history list. */
  168. NODEINFO *Info_History = NULL;
  169.  
  170. /* ?Can't have more than xx files in the indirect list? */
  171. INDIRECT_INFO indirect_list[MAX_INDIRECT_FILES];
  172.  
  173. /* The filename of the currently loaded info file. */
  174. char current_info_file[FILENAME_LEN];
  175.  
  176. /* The nodename of the node the user is looking at. */
  177. char current_info_node[NODENAME_LEN];
  178.  
  179. /* The last file actually loaded.  Not the same as current info file. */
  180. char last_loaded_info_file[FILENAME_LEN];
  181.  
  182. /* Offsets in info_file of top and bottom of current_info_node. */
  183. int nodetop, nodebot;
  184.  
  185. /* Number of lines in this node. */
  186. int nodelines;
  187.  
  188. /* Buffer for the info file. */
  189. char *info_file = NULL;
  190.  
  191. /* Length of the above buffer. */
  192. int info_buffer_len;
  193.  
  194. /* Pointer to the start of a tag table, or NULL to show none. */
  195. char *tag_table = NULL;
  196.  
  197. /* Length of the above buffer. */
  198. int tag_buffer_len;        
  199.  
  200. /* Non-zero means that the tag table is indirect. */
  201. int indirect = 0;
  202. int indirect_top;
  203.  
  204. /* Offset in the buffer of the current pagetop. */
  205. int pagetop;
  206.  
  207. /* Offset in the buffer of the last displayed character. */
  208. int pagebot = 0;
  209.  
  210. /* If non-NULL, this is a semi-colon separated list of directories to search
  211.    for a specific info file.  The user places this variable into his or
  212.    her environment. */
  213. char *infopath = NULL;
  214.  
  215. /* If filled, the name of a file to write to. */
  216. char dumpfile[FILENAME_LEN] = "";
  217.  
  218. /* This is the command to print a node. A default value is compiled in,
  219.    or it can be found from the environment as $INFO_PRINT_COMMAND. */
  220. char *print_command;
  221.  
  222. /* Non-zero means forst redisplay before prompt for the next command. */
  223. int window_bashed = 0;
  224.  
  225. /* **************************************************************** */
  226. /*                                    */
  227. /*            Getting Started.                */
  228. /*                                    */
  229. /* **************************************************************** */
  230.  
  231. /* Begin the Info session. */
  232.  
  233. /* Global is on until we are out of trouble. */
  234. int totally_inhibit_errors = 1;
  235.  
  236. struct option long_options[] = {
  237.   {"directory", 1, 0, 'd'},
  238.   {"node", 1, 0, 'n'},
  239.   {"file", 1, 0, 'f'},
  240.   {"output", 1, 0, 'o'},
  241.   {NULL, 0, NULL, 0}
  242. };
  243.  
  244. #define savestring(x) (char *) strcpy ((char *) xmalloc (1 + strlen (x)), (x))
  245.  
  246. main (argc, argv)
  247.      int argc;
  248.      char **argv;
  249. {
  250.   int c, ind;
  251.   char filename[FILENAME_LEN];
  252.   char *nodename;
  253.   char **nodenames;
  254.   int nodenames_size, nodenames_index;
  255.   char *ptr, *env_infopath, *env_print_command;
  256.  
  257.   nodenames_index = 0;
  258.   nodenames = (char **) xmalloc ((nodenames_size = 1) * sizeof (char *));
  259.   nodenames[0] = (char *)NULL;
  260.  
  261.   env_infopath = getenv ("INFOPATH");
  262.   env_print_command = getenv ("INFO_PRINT_COMMAND");
  263.  
  264.   filename[0] = '\0';
  265.  
  266.   if (env_infopath && *env_infopath)
  267.     infopath = savestring (env_infopath);
  268.   else
  269.     infopath = savestring (DEFAULT_INFOPATH);
  270.  
  271.   if (env_print_command && *env_print_command)
  272.     print_command = savestring (env_print_command);
  273.   else
  274.     print_command = savestring ("print");
  275.  
  276.   while ((c = getopt_long (argc, argv, "d:n:f:o:", long_options, &ind)) != EOF)
  277.     {
  278.       if (c == 0 && long_options[ind].flag == 0)
  279.     c = long_options[ind].val;
  280.       switch (c)
  281.     {
  282.     case 0:
  283.       break;
  284.       
  285.     case 'd':
  286.       free (infopath);
  287.       infopath = savestring (optarg);
  288.       break;
  289.       
  290.     case 'n':
  291.  
  292.       if (nodenames_index + 2 > nodenames_size)
  293.         nodenames = (char **)
  294.           xrealloc (nodenames, (nodenames_size += 10) * sizeof (char *));
  295.  
  296.       nodenames[nodenames_index++] = optarg;
  297.       nodenames[nodenames_index] = (char *)NULL;
  298.       break;
  299.       
  300.     case 'f':
  301.       strncpy (filename, optarg, FILENAME_LEN);
  302.       break;
  303.       
  304.     case 'o':
  305.       strncpy (dumpfile, optarg, FILENAME_LEN);
  306.       break;
  307.       
  308.     default:
  309.       usage ();
  310.     }
  311.     }
  312.  
  313.   /* Okay, flags are parsed.  Get possible Info menuname. */
  314.  
  315.   if (*filename && (ptr = rindex (filename,'/')) != NULL )
  316.     {
  317.       /* Add filename's directory to path. */
  318.       char *temp;
  319.  
  320.       temp = (char *) xmalloc (2 + strlen (filename) + strlen (infopath));
  321.       strncpy (temp, filename, ptr - filename);
  322.       sprintf (temp + (ptr - filename), ";%s", infopath);
  323.       free (infopath);
  324.       infopath = temp;
  325.     }
  326.  
  327.   /* Start with DIR or whatever was specified. */
  328.   if (!get_node (filename, (nodenames[0] == NULL) ? "" : nodenames[0], 0)
  329.       && !get_node ((char *)NULL, (char *)NULL, 1))
  330.       {
  331.     if (!*filename)
  332.       strcpy (filename, "dir");
  333.  
  334.     fprintf (stderr, "%s: Cannot find \"%s\", anywhere along the search ",
  335.          argv[0], filename);
  336.     fprintf (stderr, "path of\n\"%s\".\n", infopath);
  337.  
  338.     exit (1);
  339.       }
  340.  
  341.   totally_inhibit_errors = 0;
  342.  
  343.   for (ind = 1 ; ind < nodenames_index ; ind++)
  344.     get_node (filename, nodenames[ind], 0);
  345.  
  346.   nodename = nodenames[nodenames_index > 0 ? nodenames_index - 1 : 0];
  347.   if (!nodename)
  348.     {
  349.       nodename = (char *) xmalloc (NODENAME_LEN);
  350.       *nodename = '\0';
  351.     }
  352.   
  353.   if (optind != argc)
  354.     {
  355.       putchar ('\n');
  356.  
  357.       while (optind != argc)
  358.     {
  359.       if (!build_menu ())
  360.           {
  361.           display_error ("There is no menu in node \"%s\"",
  362.                  current_info_node );
  363.           break;
  364.         }
  365.       else if (!find_menu_node (argv[optind], nodename))
  366.         {
  367.           display_error
  368.         ("There is no menu entry for \"%s\" in node \"%s\"",
  369.          argv[optind], nodename );
  370.           break;
  371.         }
  372.       else if (!get_node ((char *)NULL, nodename, 0))
  373.         {
  374.           break;
  375.         }
  376.       else
  377.          {
  378. #if defined (NOTDEF)
  379.           /* RMS says not to type this stuff out because he expects
  380.          programs to call Info instead of interactive users. */
  381.           printf ("%s.. ",argv[optind]);
  382.           fflush (stdout);
  383. #endif
  384.           optind++;
  385.         }
  386.     }
  387.     }
  388.   begin_info_session ();
  389.   exit (0);
  390. }
  391.  
  392. usage ()
  393. {
  394.   fprintf (stderr,"%s\n%s\n%s\n%s\n",
  395. "Usage: info [-d dir-path] [-f info-file] [-n node-name -n node-name ...]",
  396. "            [-o output-file] [+directory dir-path] [+file info-file]",
  397. "            [+node node-name +node node-name ...]",
  398. "            [+output output-file] [menu-selection...]");
  399.   exit (1);
  400. }
  401.  
  402. #if defined (SIGTSTP)
  403. Function *old_tstp;
  404. Function *old_ttou, *old_ttin;
  405. #endif /* SIGTSTP */
  406.  
  407. #if defined (SIGWINCH)
  408. Function *old_winch;
  409. #endif /* SIGWINCH */
  410.  
  411. void info_signal_handler (int sig);
  412.  
  413. /* Start using Info. */
  414. begin_info_session ()
  415. {
  416.  
  417.   /* If the user just wants to dump the node, then do that. */
  418.   if (dumpfile[0])
  419.     {
  420.       dump_current_node (dumpfile);
  421.       exit (0);
  422.     }
  423.  
  424.   init_terminal_io ();
  425.  
  426.   /* Install handlers for restoring/breaking the screen. */
  427.  
  428.   install_signals ();
  429.   new_echo_area ();
  430.  
  431.   print_string ("Welcome to Info!  Press <F1> for help. ");
  432.   close_echo_area ();
  433.   toploop ();
  434.   goto_xy (the_window.left, the_window.bottom + 1);
  435.   restore_io ();
  436. }
  437.  
  438. /* What to do before processing a stop signal. */
  439. before_stop_signal ()
  440. {
  441.   restore_io ();
  442. }
  443.  
  444. /* What to do after processing a stop signal. */
  445. after_stop_signal ()
  446. {
  447.   clear_screen ();
  448.   display_page ();
  449.   goto_xy (the_window.ch, the_window.cv);
  450.   opsys_init_terminal ();
  451. }
  452.  
  453. /* Do the right thing with this signal. */
  454. void info_signal_handler (int sig)
  455. {
  456.   switch (sig)
  457.     {
  458. #if defined (SIGTSTP)
  459.     case SIGTSTP:
  460.     case SIGTTOU:
  461.     case SIGTTIN:
  462.       before_stop_signal ();
  463.       signal (sig, SIG_DFL);
  464. #if !defined (USG)
  465.       sigsetmask (sigblock (0) & ~sigmask (sig));
  466. #endif /* !USG */
  467.       kill (getpid (), sig);
  468.       after_stop_signal ();
  469.       signal (sig, info_signal_handler);
  470.       break;
  471. #endif /* SIGTSTP */
  472.  
  473. #if defined (SIGWINCH)
  474.     case SIGWINCH:
  475.       /* Window has changed.  Get the size of the new window, and rebuild our
  476.          window chain. */
  477.       {
  478.     int display_page ();
  479.     extern char *widest_line;
  480.     extern WINDOW terminal_window;
  481.     extern WINDOW_LIST *window_stack;
  482.     extern int terminal_rows, terminal_columns;
  483.     int delta_width, delta_height, right, bottom;
  484.  
  485.     right = get_terminal_columns ();
  486.     bottom = get_terminal_rows ();
  487.  
  488.     delta_width = right - terminal_columns;
  489.     delta_height = bottom - terminal_rows;
  490.  
  491.     terminal_columns = right;
  492.     terminal_rows = bottom;
  493.  
  494.     /* Save current window, whatever it is. */
  495.     push_window ();
  496.  
  497.     /* Change the value of the widest_line. */
  498.     free (widest_line);
  499.     widest_line = (char *) xmalloc (right);
  500.  
  501.     /* Make the new window.  Map over all windows in window list. */
  502.     {
  503.       WINDOW_LIST *wind = window_stack;
  504.       extern WINDOW modeline_window;
  505.  
  506.       while (wind != (WINDOW_LIST *)NULL)
  507.         {
  508.           adjust_wind ((WINDOW *)wind, delta_width, delta_height);
  509.           wind = wind->next_window;
  510.         }
  511.  
  512.       /* Adjust the other windows that we know about. */
  513.       adjust_wind (&terminal_window, delta_width, delta_height);
  514.       adjust_wind (&echo_area, delta_width, delta_height);
  515.       adjust_wind (&modeline_window, delta_width, delta_height);
  516.     }
  517.  
  518.     /* Clear and redisplay the entire terminal window. */
  519.     set_window (&terminal_window);
  520.     clear_screen ();
  521.  
  522.     /* Redisplay the contents of the screen. */
  523.     with_output_to_window (&terminal_window, display_page);
  524.  
  525.     /* Get back the current window. */
  526.     pop_window ();
  527.       }
  528.       break;
  529. #endif /* SIGWINCH */
  530.  
  531.     case SIGINT:
  532.       restore_io ();
  533.       exit (1);
  534.       break;
  535.     }
  536. }
  537.  
  538. install_signals ()
  539. {
  540. #if defined (SIGTSTP)
  541.   old_tstp = (Function *) signal (SIGTSTP, info_signal_handler);
  542.   old_ttou = (Function *) signal (SIGTTOU, info_signal_handler);
  543.   old_ttin = (Function *) signal (SIGTTIN, info_signal_handler);
  544. #endif /* SIGTSTP */
  545.  
  546. #if defined (SIGWINCH)
  547.   old_winch = (Function *) signal (SIGWINCH, info_signal_handler);
  548. #endif /* SIGWINCH */
  549.  
  550.   signal (SIGINT, info_signal_handler);
  551. }
  552.  
  553. adjust_wind (wind, delta_width, delta_height)
  554.      WINDOW *wind;
  555.      int delta_width, delta_height;
  556. {
  557.   wind->right += delta_width;
  558.   wind->bottom += delta_height;
  559.   wind->ch += delta_width;
  560.   wind->cv += delta_height;
  561.  
  562.   /* Ugly hack to fix busted windows code.  If the window we are adjusting
  563.      already has a TOP offset, then adjust that also. */
  564.   if (wind->top)
  565.     wind->top += delta_height;
  566. }
  567.  
  568. /* **************************************************************** */
  569. /*                                    */
  570. /*            Completing Things                */
  571. /*                                    */
  572. /* **************************************************************** */
  573.  
  574. typedef struct completion_entry
  575. {
  576.   char *identifier;
  577.   char *data;
  578.   int    position;
  579.   struct completion_entry *next;
  580. }                COMP_ENTRY;
  581.  
  582. /* The linked list of COMP_ENTRY structures that you create. */
  583. COMP_ENTRY *completion_list = (COMP_ENTRY *) NULL;
  584.  
  585. /* The vector of COMP_ENTRY pointers that COMPLETE returns. */
  586. COMP_ENTRY **completions = NULL;
  587.  
  588. /* The number of elements in the above vector. */
  589. int completion_count;
  590.  
  591. /* Initial size of COMPLETIONS array. */
  592. #define INITIAL_COMPLETIONS_CORE_SIZE 200
  593.  
  594. /* Current size of the completion array in core. */
  595. int completions_core_size = 0;
  596.  
  597. /* Ease the typing task.  Another name for the I'th
  598.    IDENTIFIER of COMPLETIONS. */
  599. #define completion_id(i) ((completions[(i)])->identifier)
  600.  
  601. /* The number of completions that can be present before the help
  602.    function starts asking you about whether it should print them
  603.    all or not. */
  604. int completion_query_threshold = 100;
  605.  
  606. free_completion_list ()
  607. {
  608.   COMP_ENTRY *temp;
  609.   while (completion_list)
  610.     {
  611.       temp = completion_list;
  612.  
  613.       if (completion_list->identifier)
  614.     free (completion_list->identifier);
  615.  
  616.       if (completion_list->data)
  617.     free (completion_list->data);
  618.  
  619.       completion_list = completion_list->next;
  620.       free (temp);
  621.     }
  622. }
  623.  
  624. /* Add a single completion to COMPLETION_LIST.
  625.    IDENTIFIER is the string that the user should type.
  626.    DATA should just be a pointer to some random data that you wish to
  627.    have associated with the identifier, but I'm too stupid for that, so
  628.    it must be a string as well.  This allocates the space for the strings
  629.    so you don't necessarily have to. */
  630. void add_completion (char *identifier, char *data, int position)
  631. {
  632.   COMP_ENTRY *temp = (COMP_ENTRY *) xmalloc (sizeof (COMP_ENTRY));
  633.  
  634.   temp->identifier = (char *) xmalloc (strlen (identifier) + 1);
  635.   strcpy (temp->identifier, identifier);
  636.  
  637.   temp->data = (char *) xmalloc (strlen (data) + 1);
  638.   strcpy (temp->data, data);
  639.  
  640.   temp->position = position;
  641.   temp->next = completion_list;
  642.   completion_list = temp;
  643. }
  644.  
  645. /* Function for reading a line.  Supports completion on COMPLETION_LIST
  646.    if you pass COMPLETING as non-zero.  Prompt is either a prompt or
  647.    NULL, LINE is the place to store the characters that are read.
  648.    LINE may start out already containing some characters; if so, they
  649.    are printed.  MAXCHARS tells how many characters can fit in the
  650.    buffer at LINE.  readline () returns zero if the user types the
  651.    abort character.  LINE is returned with a '\0' at the end, not a '\n'. */
  652. int
  653. readline (prompt, line, maxchars, completing)
  654.      char *prompt, *line;
  655.      int maxchars;
  656.      int completing;
  657. {
  658.   int character;
  659.   int readline_ch, readline_cv;
  660.   int current_len = strlen (line);
  661.   int just_completed = 0;        /* Have we just done a completion? */
  662.  
  663.   new_echo_area ();
  664.  
  665.   if (prompt)
  666.     print_string ("%s", prompt);
  667.  
  668.   readline_ch = the_window.ch;
  669.   readline_cv = the_window.cv;
  670.  
  671.   print_string ("%s", line);
  672.  
  673.   while (1)
  674.     {
  675.       line[current_len] = '\0';
  676.       goto_xy (readline_ch, readline_cv);
  677.       print_string ("%s", line);
  678.       clear_eol ();
  679.  
  680.       if (just_completed)
  681.     just_completed--;
  682.  
  683.       character = blink_cursor ();
  684.  
  685.       switch (character)
  686.     {
  687.     case EOF:
  688.       character = '\n';
  689.  
  690.     case CTRL ('W'):
  691.       while (current_len && line[current_len] == SPACE)
  692.         current_len--;
  693.  
  694.       if (!current_len)
  695.         break;
  696.  
  697.       while (current_len && line[current_len] != SPACE)
  698.         current_len--;
  699.  
  700.       break;
  701.  
  702.     case CTRL ('U'):
  703.       current_len = 0;
  704.       break;
  705.  
  706.     case '\b':
  707.     case 0x07f:
  708.       if (current_len)
  709.         current_len--;
  710.       else
  711.         ding ();
  712.       break;
  713.  
  714.     case KUpArrow:
  715.         if (completing)
  716.         {
  717.         }
  718.         break;
  719.     case KDownArrow:
  720.         if (completing)
  721.         {
  722.         }
  723.         break;
  724.  
  725.     case '\n':
  726.     case '\r':
  727.       if (completing)
  728.         {
  729.           extern int completion_count;
  730.  
  731.           try_complete (line);
  732.  
  733.           if (completion_count >= 1)
  734.         {
  735.           close_echo_area ();
  736.           return (1);
  737.         }
  738.           else
  739.         {
  740.           current_len = strlen (line);
  741.           break;
  742.         }
  743.         }
  744.       else
  745.         {
  746.           close_echo_area ();
  747.           return (1);
  748.         }
  749.  
  750.     case KEsc:
  751.     case ABORT_CHAR:
  752.       ding ();
  753.  
  754.       if (current_len)
  755.         {
  756.           current_len = 0;
  757.         }
  758.       else
  759.         {
  760.           close_echo_area ();
  761.           clear_echo_area ();
  762.           return (0);
  763.         }
  764.       break;
  765.  
  766.     case ' ':
  767.     case '\t':
  768.     case '?':
  769.       if (completing)
  770.         {
  771.           extern int completion_count;
  772.  
  773.           if (character == '?' || just_completed)
  774.         {
  775.           help_possible_completions (line);
  776.           break;
  777.         }
  778.           else
  779.         {
  780.           char temp_line[NODENAME_LEN];
  781.           strcpy (temp_line, line);
  782.           try_complete (line); just_completed = 2;
  783.           if (completion_count != 1 && character == SPACE)
  784.             {
  785.               if (strcmp (temp_line, line) == 0)
  786.             {
  787.               line[current_len] = SPACE;
  788.               line[current_len + 1] = '\0';
  789.               strcpy (temp_line, line);
  790.               try_complete (line);
  791.               if (completion_count == 0)
  792.                 {
  793.                   line[current_len] = '\0';
  794.                   ding ();
  795.                 }
  796.             }
  797.             }
  798.           current_len = strlen (line);
  799.           if (completion_count == 0)
  800.             ding ();
  801.           break;
  802.         }
  803.         }
  804.       /* Do *NOT* put anything in-between the completing cases and
  805.          the default: case.  No.  Because the SPC, TAB and `?' get
  806.          treated as normal characters by falling through the
  807.          "if (completing)" test above. */
  808.     default:
  809.       if (!CTRL_P (character) &&
  810.           current_len < maxchars)
  811.         line[current_len++] = character;
  812.       else
  813.         ding ();
  814.     }
  815.     }
  816. }
  817.  
  818. /* Initialize whatever the completer is using. */
  819. init_completer ()
  820. {
  821.   if (completions_core_size != INITIAL_COMPLETIONS_CORE_SIZE)
  822.     {
  823.       if (completions)
  824.     free (completions);
  825.  
  826.       completions = (COMP_ENTRY **)
  827.     xmalloc ((sizeof (COMP_ENTRY *))
  828.          * (completions_core_size = INITIAL_COMPLETIONS_CORE_SIZE));
  829.     }
  830.   completion_count = 0;
  831. }
  832.  
  833. /* Reverse the completion list passed in LIST, and
  834.    return a pointer to the new head. */
  835. COMP_ENTRY *
  836. reverse_list (list)
  837.      COMP_ENTRY *list;
  838. {
  839.   COMP_ENTRY *next;
  840.   COMP_ENTRY *prev = (COMP_ENTRY *) NULL;
  841.  
  842.   while (list)
  843.     {
  844.       next = list->next;
  845.       list->next = prev;
  846.       prev = list;
  847.       list = next;
  848.     }
  849.   return (prev);
  850. }
  851.  
  852. /* Remember the possible completion passed in POINTER on the
  853.    completions list. */
  854. remember_completion (pointer)
  855.      COMP_ENTRY *pointer;
  856. {
  857.   if (completion_count == completions_core_size)
  858.     {
  859.       COMP_ENTRY **temp = (COMP_ENTRY **)
  860.     realloc (completions, ((sizeof (COMP_ENTRY *))
  861.                    * (completions_core_size +=
  862.                   INITIAL_COMPLETIONS_CORE_SIZE)));
  863.       if (!temp)
  864.     {
  865.       display_error ("Too many completions (~d)!  Out of core!",
  866.              completion_count);
  867.       return;
  868.     }
  869.       else
  870.     completions = temp;
  871.     }
  872.   completions[completion_count++] = pointer;
  873. }
  874.  
  875. /* Complete TEXT from identifiers in LIST.  Place the resultant
  876.    completions in COMPLETIONS, and the number of completions in
  877.    COMPLETION_COUNT. Modify TEXT to contain the least common
  878.    denominator of all the completions found. */
  879. int
  880. complete (text, list)
  881.      char *text;
  882.      COMP_ENTRY *list;
  883. {
  884.   int low_match, i, idx;
  885.   int string_length = strlen (text);
  886.  
  887.   init_completer ();
  888.   low_match = 100000;        /* Some large number. */
  889.  
  890.   while (list)
  891.     {
  892.       if (strnicmp (text, list->identifier, string_length) == 0)
  893.     remember_completion (list);
  894.       list = list->next;
  895.     }
  896.  
  897.   if (completion_count == 0)
  898.     return (0);
  899.  
  900.   if (completion_count == 1)
  901.     {                /* One completion */
  902.       strcpy (text, completion_id (0));
  903.       return (1);
  904.     }
  905.  
  906.   /* Else find the least common denominator */
  907.  
  908.   idx = 1;
  909.  
  910.   while (idx < completion_count)
  911.     {
  912.       int c1, c2;
  913.       for (i = 0;
  914.        (c1 = to_lower (completion_id (idx - 1)[i])) &&
  915.        (c2 = to_lower (completion_id (idx)[i]));
  916.        i++)
  917.     if (c1 != c2)
  918.       break;
  919.  
  920.       if (low_match > i)
  921.     low_match = i;
  922.       idx++;
  923.     }
  924.  
  925.   strncpy (text, completion_id (0), low_match);
  926.   text[low_match] = '\0';
  927.   return (1);
  928. }
  929.  
  930. /* Complete TEXT from the completion structures in COMPLETION_LIST. */
  931. int
  932. try_complete (text)
  933.      char *text;
  934. {
  935.   return (complete (text, completion_list));
  936. }
  937.  
  938. /* The function that prints out the possible completions. */
  939. help_possible_completions (text)
  940.      char *text;
  941. {
  942.   char temp_string[2000];
  943.  
  944.   goto_xy (the_window.left, the_window.top);
  945.   strcpy (temp_string, text);
  946.   try_complete (temp_string);
  947.  
  948.   open_typeout ();
  949.  
  950.   if (completion_count == 0)
  951.     {
  952.       print_string ("There are no possible completions.\n");
  953.       goto print_done;
  954.     }
  955.  
  956.   if (completion_count == 1)
  957.     {
  958.       print_string
  959.     ("The only possible completion of what you have typed is:\n\n");
  960.       print_string ("%s", completion_id(0));
  961.       goto print_done;
  962.     }
  963.  
  964.   if (completion_count >= completion_query_threshold)
  965.     {
  966.       print_string
  967.     ("\nThere are %d completions.  Do you really want to see them all",
  968.      completion_count);
  969.  
  970.       if (!get_y_or_n_p ())
  971.     return;
  972.     }
  973.  
  974.   print_string ("\nThe %d completions of what you have typed are:\n\n",
  975.         completion_count);
  976.  
  977.   {
  978.     int idx = 0;
  979.     int counter = 0;
  980.     int columns = (the_window.right - the_window.left) / 30;
  981.  
  982.     while (idx < completion_count)
  983.       {
  984.     if (counter == columns)
  985.       {
  986.         charout ('\n');
  987.         counter = 0;
  988.       }
  989.     indent_to (counter * 30);
  990.     print_string ("%s", completion_id (idx));
  991.     counter++;
  992.     idx++;
  993.       }
  994.   }
  995.  
  996. print_done:
  997.   print_string ("\n\n-----------------\n");
  998.   close_typeout ();
  999. }
  1000.  
  1001. /* Return the next file that should be searched, or NULL if we are at the end
  1002.    of the info file. If the FILE argument is provided, begin the search there,
  1003.    if REWIND is non-zero start the search at the beginning of the list.
  1004.  
  1005.    The list is the one built by an indirect tag table, on the supposition
  1006.    that those files form a logical set to search if we are in one of them.
  1007.    If no such list is current (either it doesn't exist, or FILE isn't on
  1008.    it) the search list is set to be last_loaded_info_file */
  1009. char *
  1010. next_info_file (file, rewind)
  1011.      char *file;        /* file to set `next' to. May be NULL. */
  1012.      int rewind;        /* should I rewind the file list?  */
  1013. {
  1014.   static int index = -1;
  1015.  
  1016.   if (file != NULL)
  1017.     {
  1018.       char *ptr = rindex (file,'/');
  1019.  
  1020.       if (ptr != NULL)
  1021.     file = ptr + 1;
  1022.  
  1023.       for (index = 0;
  1024.        index < MAX_INDIRECT_FILES &&
  1025.        indirect_list[index].filename != (char *)NULL;
  1026.        index++)
  1027.     {
  1028.       if (strcmp (file, indirect_list[index].filename) == 0)
  1029.         return (file);
  1030.     }
  1031.  
  1032.       /* OK, we are not on the current indirect_list. This means that
  1033.      we have switched to another node that has no indirect list,
  1034.      so forget the old one. */
  1035.       for (index = 0;
  1036.        index < MAX_INDIRECT_FILES &&
  1037.        indirect_list[index].filename != (char *)NULL;
  1038.        index++)
  1039.     {
  1040.       free (indirect_list[index].filename);
  1041.       indirect_list[index].filename = (char *)NULL;
  1042.     }
  1043.       return (indirect_list[0].filename = savestring (file));
  1044.     }
  1045.   else if (rewind)
  1046.     {
  1047.       index = 0;
  1048.  
  1049.       if (indirect_list[0].filename == (char *)NULL)
  1050.     indirect_list[0].filename = savestring (last_loaded_info_file);
  1051.     }
  1052.   else
  1053.     index++;
  1054.   
  1055.   if (index < MAX_INDIRECT_FILES &&
  1056.       indirect_list[index].filename != (char *)NULL)
  1057.     return (indirect_list[index].filename);
  1058.  
  1059.   index = -1;
  1060.   return (NULL);
  1061. }
  1062.  
  1063. /* **************************************************************** */
  1064. /*                                    */
  1065. /*            Getting Nodes                    */
  1066. /*                                    */
  1067. /* **************************************************************** */
  1068.  
  1069. /* A node name looks like:
  1070.    Node: nodename with spaces but not a comma,
  1071. or Node: (filename-containing-node)node-within-file
  1072. or Node: (filename)
  1073.  
  1074.    The latter case implies a nodename of "Top".  All files are
  1075.    supposed to have one.
  1076.  
  1077.    Lastly, the nodename specified could be "*", which specifies the
  1078.    entire file. */
  1079.  
  1080. /* Return the directory portion of FILENAME, i.e., everything before the
  1081.    last slash. */
  1082. static char *
  1083. file_directory (filename)
  1084.      char *filename;
  1085. {
  1086.   register char *scan;
  1087.   register int length;
  1088.   char *result;
  1089.  
  1090.   scan = filename;
  1091.  
  1092.   while (*scan++ != '\0');
  1093.  
  1094.   while (1)
  1095.     {
  1096.       if (scan == filename)
  1097.     break;
  1098.  
  1099.       if ((*--scan) == '/')
  1100.     {
  1101.       scan++;
  1102.       break;
  1103.     }
  1104.     }
  1105.  
  1106.   length = scan - filename;
  1107.   result = (char *) xmalloc (length + 1);
  1108.   strncpy (result, filename, length);
  1109.   result[length] = '\0';
  1110.  
  1111.   return (result);
  1112. }
  1113.  
  1114. /* Given FILENAME and DIRECTORY return a newly allocated string which
  1115.    is either the two concatenated, or simply FILENAME if it is absolute
  1116.    already. */
  1117. static char *
  1118. file_absolutize (filename, directory)
  1119.      char *filename, *directory;
  1120. {
  1121.   register int filename_len, directory_len;
  1122.   char *result;
  1123.  
  1124.   if (filename[0] == '/')
  1125.     return (savestring (filename));
  1126.  
  1127.   filename_len = strlen (filename);
  1128.   directory_len = strlen (directory);
  1129.   result = (char *) xmalloc (directory_len + filename_len + 1);
  1130.  
  1131.   strcpy (result, directory);
  1132.   strcat (result, filename);
  1133.  
  1134.   return (result);
  1135. }
  1136.  
  1137. /* Load FILENAME.  If REMEMBER_NAME is non-zero, then remember the
  1138.    loaded filename in CURRENT_INFO_FILE.  In either case, remember
  1139.    the name of this file in LAST_LOADED_INFO_FILE. */
  1140. int
  1141. get_info_file (filename, remember_name)
  1142.      char *filename;
  1143.      int remember_name;
  1144. {
  1145.   FILE *input_stream;
  1146.   struct stat file_info;
  1147.   int pointer, result;
  1148.   char tempname[FILENAME_LEN];
  1149.  
  1150.   /* Get real filename. */
  1151.   strcpy (tempname, opsys_filename (filename));
  1152.  
  1153.   /* See if the file exists. */
  1154.   if ((result = stat (tempname, &file_info)) != 0)
  1155.     {
  1156.       /* Try again, this time with the name in lower-case. */
  1157.       char temp_tempname[FILENAME_LEN];
  1158.       int i;
  1159.  
  1160.       for (i = 0; temp_tempname[i] = to_lower (tempname[i]); i++);
  1161.       strcpy (temp_tempname, opsys_filename (temp_tempname));
  1162.  
  1163.       result = stat (temp_tempname, &file_info);
  1164.       if (!result)
  1165.     strcpy (tempname, temp_tempname);
  1166.     }
  1167.  
  1168.   /* See if this file is the last loaded one. */
  1169.   if (!result && (strcmp (last_loaded_info_file, tempname) == 0))
  1170.     return (1);
  1171.  
  1172.   /* Now try to open the file. */
  1173.   if (result || (input_stream = fopen (tempname, "rt")) == NULL)
  1174.     {
  1175.       file_error (tempname);
  1176.       return (0);
  1177.     }
  1178.  
  1179.   /* If we already have a file loaded, then free it first. */
  1180.   if (info_file)
  1181.     {
  1182.       free (info_file);
  1183.  
  1184.       if (!indirect)
  1185.     {
  1186.       /* Then the tag table is also no longer valid. */
  1187.       tag_table = (char *) NULL;
  1188.     }
  1189.     }
  1190.  
  1191.   /* Read the contents of the file into a new buffer. */
  1192.  
  1193.   info_file = (char *) xmalloc (info_buffer_len = file_info.st_size);
  1194.   info_buffer_len = fread (info_file, 1, info_buffer_len, input_stream);
  1195.   fclose (input_stream);
  1196.   strcpy (last_loaded_info_file, tempname);
  1197.   if (remember_name)
  1198.     {
  1199.       strcpy (current_info_file, tempname);
  1200.       if (indirect)
  1201.     {
  1202.       int idx;
  1203.       indirect = 0;
  1204.       free (tag_table);
  1205.     }
  1206.     }
  1207.   else
  1208.     return (1);
  1209.  
  1210.   /* Force redisplay, since we are looking at a new file. */
  1211.   window_bashed = 1;
  1212.  
  1213.   /* The file has been read, and we don't know anything about it.
  1214.      Find out if it contains a tag table. */
  1215.  
  1216.   tag_table = NULL;        /* assume none. */
  1217.   indirect = 0;
  1218.   tag_buffer_len = 0;
  1219.  
  1220.   set_search_constraints (info_file, info_buffer_len);
  1221.  
  1222.   /* Go to the last few lines in the file. */
  1223.   pointer = back_lines (8, info_buffer_len);
  1224.   pointer = search_forward (TAG_TABLE_END_STRING, pointer);
  1225.  
  1226.   if (pointer > -1)
  1227.     {
  1228.       /* Then there is a tag table.  Find the start of it,
  1229.      and remember that. */
  1230.       pointer = search_backward (TAG_TABLE_BEG_STRING, pointer);
  1231.  
  1232.       /* Handle error for malformed info file. */
  1233.       if (pointer < 0)
  1234.     display_error ("Start of tag table not found!");
  1235.       else
  1236.     {
  1237.       /* No problem.  If this file is an indirect file, then the contents
  1238.          of the tag table must remain in RAM the entire time.  Otherwise,
  1239.          we can flush the tag table with the file when the file is flushed.
  1240.          So, if indirect, remember that, and copy the table to another
  1241.          place.*/
  1242.  
  1243.       int indirect_check = forward_lines (2, pointer);
  1244.  
  1245.       tag_table = info_file + pointer;
  1246.       tag_buffer_len = info_buffer_len - pointer;
  1247.  
  1248.       /* Shorten the search constraints. */
  1249.       info_buffer_len = pointer;
  1250.  
  1251.       if (looking_at ("(Indirect)\n", indirect_check))
  1252.         {
  1253.           /* We have to find the start of the indirect file's
  1254.          information. */
  1255.           tag_table = (char *) xmalloc (tag_buffer_len);
  1256.  
  1257.           bcopy (&info_file[indirect_check], tag_table, tag_buffer_len);
  1258.  
  1259.           /* Find the list of filenames. */
  1260.           indirect_top = search_backward ("Indirect:\n", indirect_check);
  1261.           if (indirect_top < 0)
  1262.         {
  1263.           free (tag_table);
  1264.           tag_table = (char *) NULL;
  1265.           display_error ("Start of INDIRECT tag table not found!");
  1266.           return (0);
  1267.         }
  1268.  
  1269.           /* Remember the filenames, and their byte offsets. */
  1270.           {
  1271.         /* Index into the filename/offsets array. */
  1272.         int idx, temp_first_byte;
  1273.         char temp_filename[FILENAME_LEN];
  1274.         char *directory = file_directory (tempname);
  1275.  
  1276.         info_buffer_len = indirect_top;
  1277.  
  1278.         /* For each line, scan the info into globals.  Then save
  1279.                the information in the INDIRECT_INFO structure. */
  1280.  
  1281.         for (idx = 0; idx < MAX_INDIRECT_FILES &&
  1282.              indirect_list[idx].filename != (char *) NULL;
  1283.              idx++)
  1284.             {
  1285.              free (indirect_list[idx].filename);
  1286.              indirect_list[idx].filename = (char *) NULL;
  1287.           }
  1288.         
  1289.         for (idx = 0;info_file[indirect_top] != info_separator_char &&
  1290.              idx < MAX_INDIRECT_FILES;)
  1291.           {
  1292.             indirect_top = forward_lines (1, indirect_top);
  1293.             if (info_file[indirect_top] == info_separator_char)
  1294.               break;
  1295.  
  1296.             /* Ignore blank lines. */
  1297.             if (info_file[indirect_top] == '\n')
  1298.               continue;
  1299.  
  1300.             sscanf (&info_file[indirect_top], "%s%d",
  1301.                 temp_filename, &temp_first_byte);
  1302.  
  1303.             if (strlen (temp_filename))
  1304.               {
  1305.             temp_filename[strlen (temp_filename) - 1] = '\0';
  1306.             indirect_list[idx].filename =
  1307.               file_absolutize (temp_filename, directory);
  1308.             indirect_list[idx].first_byte = temp_first_byte;
  1309.             idx++;
  1310.               }
  1311.           }
  1312.  
  1313.         free (directory);
  1314.  
  1315.         /* Terminate the table. */
  1316.         if (idx == MAX_INDIRECT_FILES)
  1317.           {
  1318.             display_error
  1319.               ("Sorry, the INDIRECT file array isn't large enough.");
  1320.             idx--;
  1321.           }
  1322.         indirect_list[idx].filename = (char *) NULL;
  1323.           }
  1324.           indirect = 1;
  1325.        } else {
  1326.           ;
  1327.        }
  1328.     }
  1329.     }
  1330.   return (1);
  1331. }
  1332.  
  1333. /* Make current_info_node be NODENAME.  This could involve loading
  1334.    a file, etc.  POPPING is non-zero if we got here because we are
  1335.    popping one level. */
  1336. int
  1337. get_node (filename, nodename, popping)
  1338.      char *nodename, *filename;
  1339.      int popping;
  1340. {
  1341.   int pointer;
  1342.   char internal_filename[FILENAME_LEN];
  1343.   char internal_nodename[NODENAME_LEN];
  1344.  
  1345.   if (nodename && *nodename)
  1346.     {
  1347.       /* Maybe nodename looks like: (filename)nodename, or worse: (filename).
  1348.          If so, extract the stuff out. */
  1349.       if (*nodename == '(')
  1350.     {
  1351.       int temp = 1, temp1 = 0;
  1352.       char character;
  1353.  
  1354.       filename = internal_filename;
  1355.  
  1356.       while ((character = nodename[temp]) && character != ')')
  1357.         {
  1358.           filename[temp - 1] = character;
  1359.           temp++;
  1360.         }
  1361.       filename[temp - 1] = '\0';
  1362.       temp++;            /* skip the closing ')' */
  1363.  
  1364.       /* We have the filename now.  The nodename follows. */
  1365.       internal_nodename[0] = '\0';
  1366.  
  1367.       while (nodename[temp] == ' ' ||
  1368.          nodename[temp] == '\t' ||
  1369.          nodename[temp] == '\n')
  1370.         temp++;
  1371.  
  1372.       if (nodename[temp])
  1373.         while (internal_nodename[temp1++] = nodename[temp++])
  1374.           ;
  1375.       else if (*filename != '\0')
  1376.         strcpy (internal_nodename,"Top");
  1377.  
  1378.       nodename = internal_nodename;
  1379.     }
  1380.     }
  1381.  
  1382.   if (!popping)
  1383.     push_node (current_info_file, current_info_node, pagetop, nodetop);
  1384.  
  1385.   if (!nodename || !*nodename)
  1386.     {
  1387.       nodename = internal_nodename;
  1388.       strcpy (nodename, "Top");
  1389.     }
  1390.  
  1391.   if (!filename || !*filename)
  1392.     {
  1393.       filename = internal_filename;
  1394.       strcpy (filename, current_info_file);
  1395.     }
  1396.  
  1397.   if (!*filename)
  1398.     strcpy (filename, "dir");
  1399.  
  1400.   if (!get_info_file (filename, 1))
  1401.     goto node_not_found;
  1402.  
  1403.   if (strcmp (nodename, "*") == 0)
  1404.     {
  1405.       /* The "node" that we want is the entire file. */
  1406.       pointer = 0;
  1407.       goto found_node;
  1408.     }
  1409.   
  1410.   /* If we are using a tag table, see if we can find the nodename in it. */
  1411.   if (tag_table)
  1412.     {
  1413.       pointer = find_node_in_tag_table (nodename, 0);
  1414.       if (pointer < 1)
  1415.     {
  1416.       int pop_node ();
  1417.  
  1418.       /* The search through the tag table failed.  Maybe we
  1419.          should try searching the buffer?  Nahh, just barf. */
  1420.     node_not_found:
  1421.       if (popping)
  1422.         return (0);    /* Second time through. */
  1423.  
  1424.       {
  1425.          int save_inhibit = totally_inhibit_errors;
  1426.  
  1427.          totally_inhibit_errors = 0;
  1428.          display_error
  1429.            ("Sorry, unable to find the node \"%s\" in the file \"%s\".",
  1430.         nodename, filename);
  1431.          totally_inhibit_errors = save_inhibit;
  1432.       }
  1433.  
  1434.       current_info_file[0] = '\0';
  1435.       current_info_node[0] = '\0';
  1436.       last_loaded_info_file[0] = '\0';
  1437.       pop_node (internal_filename, internal_nodename, &nodetop, &pagetop);
  1438.       get_node (internal_filename, internal_nodename, 1);
  1439.       return (0);
  1440.     }
  1441.  
  1442.       /* Otherwise, the desired location is right here.
  1443.          Scarf the position byte. */
  1444.       while (tag_table[pointer] != '\177')
  1445.     pointer++;
  1446.  
  1447.       sscanf (&tag_table[pointer + 1], "%d", &pointer);
  1448.  
  1449.       /* Okay, we have a position pointer.  If this is an indirect file,
  1450.          then we should look through the indirect_list for the first
  1451.          element.first_byte which is larger than this.  Then we can load
  1452.          the specified file, and win. */
  1453.       if (indirect)
  1454.     {
  1455.       /* Find the filename for this node. */
  1456.       int idx;
  1457.       for (idx = 0; idx < MAX_INDIRECT_FILES &&
  1458.            indirect_list[idx].filename != (char *) NULL; idx++)
  1459.         {
  1460.           if (indirect_list[idx].first_byte > pointer)
  1461.         {
  1462.           /* We found it. */
  1463.           break;
  1464.         }
  1465.         }
  1466.       if (!get_info_file (indirect_list[idx - 1].filename, 1))
  1467.         goto node_not_found;
  1468.       pointer -= indirect_list[idx - 1].first_byte;
  1469.  
  1470.       /* Here is code to compensate for the header of an indirect file. */
  1471.       {
  1472.         int tt = find_node_start (0);
  1473.         if (tt > -1)
  1474.           pointer += tt;
  1475.       }
  1476.     }
  1477.       else
  1478.     {
  1479.       /* This tag table is *not* indirect.  The filename of the file
  1480.          containing this node is the same as the current file.  The
  1481.          line probably looks like:
  1482.          File: info,  Node: Checking25796 */
  1483.     }
  1484.     }
  1485.   else
  1486.     {
  1487.       /* We don't have a tag table.  The node can only be found by
  1488.          searching this file in its entirety.  */
  1489.       if (!get_info_file (filename, 1))
  1490.     return (0);
  1491.  
  1492.       pointer = 0;
  1493.     }
  1494.  
  1495.   /* Search this file, using pointer as a good guess where to start. */
  1496.   /* This is the same number that RMS used.  It might be right or wrong. */
  1497.   pointer -= 1000;
  1498.   if (pointer < 0)
  1499.     pointer = 0;
  1500.  
  1501.   pointer = find_node_in_file (nodename, pointer);
  1502.   if (pointer < 0)
  1503.     goto node_not_found;
  1504.  
  1505.   /* We found the node in its file.  Remember exciting information. */
  1506.  
  1507. found_node:
  1508.   back_lines (0, pointer);
  1509.   nodetop = pagetop = pointer;
  1510.   strcpy (current_info_node, nodename);
  1511.   strcpy (current_info_file, filename);
  1512.   get_node_extent ();
  1513.   return (1);
  1514. }
  1515.  
  1516. /* Get the bounds for this node.  NODETOP points to the start of the
  1517.    node. Scan forward looking for info_separator_char, and remember
  1518.    that in NODEBOT. */
  1519. get_node_extent ()
  1520. {
  1521.   int idx = nodetop;
  1522.   int character;
  1523.   int do_it_till_end = (strcmp (current_info_node, "*") == 0);
  1524.  
  1525.   nodelines = 0;
  1526.  
  1527. again:
  1528.   while ((idx < info_buffer_len) &&
  1529.      ((character = info_file[idx]) != info_separator_char))
  1530.     {
  1531.       if (character == '\n')
  1532.     nodelines++;
  1533.       idx++;
  1534.     }
  1535.   if (do_it_till_end && idx != info_buffer_len)
  1536.     {
  1537.       idx++;
  1538.       goto again;
  1539.     }
  1540.   nodebot = idx;
  1541. }
  1542.  
  1543. /* Locate the start of a node in the current search_buffer.  Return
  1544.    the offset to the node start, or minus one.  START is the place in
  1545.    the file at where to begin the search. */
  1546. find_node_start (start)
  1547.      int start;
  1548. {
  1549.   return (search_forward (start_of_node_string, start));
  1550. }
  1551.  
  1552. /* Find NODENAME in TAG_TABLE. */
  1553. find_node_in_tag_table (nodename, offset)
  1554.      char *nodename;
  1555.      int offset;
  1556. {
  1557.   int temp;
  1558.  
  1559.   set_search_constraints (tag_table, tag_buffer_len);
  1560.  
  1561.   temp = offset;
  1562.   while (1)
  1563.     {
  1564.       offset = search_forward (NODE_ID, temp);
  1565.  
  1566.       if (offset < 0)
  1567.     return (offset);
  1568.  
  1569.       temp = skip_whitespace (offset + strlen (NODE_ID));
  1570.  
  1571.       if (strnicmp (tag_table + temp, nodename, strlen (nodename)) == 0)
  1572.     if (*(tag_table + temp + strlen (nodename)) == '\177')
  1573.       return (temp);
  1574.     }
  1575. }
  1576.  
  1577. /* Find NODENAME in INFO_FILE. */
  1578. find_node_in_file (nodename, offset)
  1579.      char *nodename;
  1580.      int offset;
  1581. {
  1582.   int temp, last_offset = -1;
  1583.  
  1584.   set_search_constraints (info_file, info_buffer_len);
  1585.  
  1586.   while (1)
  1587.     {
  1588.       offset = find_node_start (offset);
  1589.  
  1590.       if (offset == last_offset)
  1591.     offset = -1;
  1592.       else
  1593.     last_offset = offset;
  1594.  
  1595.       if (offset < 0)
  1596.     return (offset);
  1597.       else
  1598.     temp = forward_lines (1, offset);
  1599.  
  1600.       if (temp == offset)
  1601.     return (-1);        /* At last line now, just a node start. */
  1602.       else
  1603.     offset = temp;
  1604.  
  1605.       temp = string_in_line (NODE_ID, offset);
  1606.  
  1607.       if (temp > -1)
  1608.     {
  1609.       temp = skip_whitespace (temp + strlen (NODE_ID));
  1610.       if (strnicmp (info_file + temp, nodename, strlen (nodename)) == 0)
  1611.         {
  1612.           int check_exact = *(info_file + temp + strlen (nodename));
  1613.  
  1614.           if (check_exact == '\t' ||
  1615.           check_exact == ',' ||
  1616.           check_exact == '.' ||
  1617.           check_exact == '\n')
  1618.         return (offset);
  1619.         }
  1620.     }
  1621.     }
  1622. }
  1623.  
  1624.  
  1625. /* **************************************************************** */
  1626. /*                                    */
  1627. /*            Dumping and Printing Nodes                */
  1628. /*                                    */
  1629. /* **************************************************************** */
  1630.  
  1631. /* Make a temporary filename based on STARTER and the PID of this Info. */
  1632. char *
  1633. make_temp_filename (starter)
  1634.      char *starter;
  1635. {
  1636.   register int i;
  1637.   char *temp;
  1638.  
  1639.   temp = (char *) xmalloc (strlen (starter) + 10);
  1640.   sprintf (temp, "%s-%d", starter, getpid ());
  1641.  
  1642.   for (i = 0; temp[i]; i++)
  1643.     if (!isalnum (temp[i]))
  1644.       temp[i] = '-';
  1645.  
  1646.   return (temp);
  1647. }
  1648.  
  1649. /* Delete a file.  Print errors if necessary. */
  1650. deletefile (filename)
  1651.      char *filename;
  1652. {
  1653.   if (unlink (filename) != 0)
  1654.     {
  1655.       file_error (filename);
  1656.       return (1);
  1657.     }
  1658.   return (0);
  1659. }
  1660.  
  1661. printfile (filename)
  1662.      char *filename;
  1663. {
  1664.   int length = strlen (print_command) + strlen (filename) + strlen ("\n") + 1;
  1665.   char *command = (char *) xmalloc (length);
  1666.   int error;
  1667.  
  1668.   display_error ("Printing file `%s'...\n", filename);
  1669.   sprintf (command, "%s %s", print_command, filename);
  1670.   error = system (command);
  1671.   if (error)
  1672.     display_error ("Can't invoke `%s'", command);
  1673.   free (command);
  1674.   return (error);
  1675. }
  1676.  
  1677. /* Dump the current node into a file named FILENAME.
  1678.    Return 0 if the dump was successful, otherwise,
  1679.    print error and exit. */
  1680. dump_current_node (filename)
  1681.      char *filename;
  1682. {
  1683.   int i = nodetop;
  1684.   int c;
  1685.   FILE *output_stream = fopen (filename, "wt");
  1686.   if (output_stream == (FILE *) NULL)
  1687.     {
  1688.       file_error (filename);
  1689.       return (1);
  1690.     }
  1691.  
  1692.   while (i < nodebot && i < info_buffer_len)
  1693.     {
  1694.       c = info_file[i];
  1695.       if (CTRL_P (c) && !(index ("\n\t\f", c)))
  1696.     {
  1697.       putc ('^', output_stream);
  1698.       c = UNCTRL (c);
  1699.     }
  1700.       if (putc (c, output_stream) == EOF)
  1701.     {
  1702.       fclose (output_stream);
  1703.       file_error (filename);
  1704.       return (1);
  1705.     }
  1706.       i++;
  1707.     }
  1708.   fclose (output_stream);
  1709.   return (0);
  1710. }
  1711.  
  1712. /* **************************************************************** */
  1713. /*                                    */
  1714. /*             Toplevel eval loop.                 */
  1715. /*                                    */
  1716. /* **************************************************************** */
  1717.  
  1718. #define MENU_HEADER "\n* Menu:"
  1719. #define MENU_ID "\n* "
  1720. #define FOOTNOTE_HEADER "*Note"
  1721.  
  1722. /* Number of items that the current menu has. */
  1723. int the_menu_size = 0;
  1724.  
  1725. /* The node that last made a menus completion list. */
  1726. char menus_nodename[NODENAME_LEN];
  1727. char menus_filename[NODENAME_LEN];
  1728.  
  1729. static int search_start = 0;
  1730.  
  1731. void bash_window_to(int position)
  1732. {
  1733.     int pointer;
  1734.  
  1735.     pointer = back_lines ((the_window.bottom - the_window.top) / 2
  1736.         , forward_lines (1, position));
  1737.  
  1738.     if (pointer < nodetop)
  1739.         pointer = nodetop;
  1740.  
  1741.     pagetop = pointer;
  1742.     window_bashed = 1;
  1743. }
  1744.  
  1745. /* The default prompt string for the Follow Reference command. */
  1746. char *visible_footnote = (char *)NULL;
  1747. toploop ()
  1748. {
  1749.     int done, inhibit_display, new_node, menu_item;
  1750.     int command, last_command;
  1751.     int last_pointer, count, new_ypos, last_pagetop;
  1752.     char nodename[NODENAME_LEN];
  1753.     char menuname[NODENAME_LEN];
  1754.  
  1755.     done = inhibit_display = 0;
  1756.     command = last_command = 0;
  1757.     menu_item = new_node = new_ypos = last_pagetop = -1;
  1758.     menuname[0] = '\0';
  1759.  
  1760.     while (!done)
  1761.        {
  1762.         if (!inhibit_display && (window_bashed || (pagetop != last_pagetop)))
  1763.             display_page ();
  1764.  
  1765.         if (new_node)
  1766.         {
  1767.             build_menu();
  1768.             menu_item = -1;
  1769.             menuname[0] = '\0';
  1770.             new_node = 0;
  1771.         }
  1772.         if (menu_item < 0 || new_ypos < pagetop || new_ypos >= pagebot)
  1773.         {
  1774.               COMP_ENTRY *mp = completion_list;
  1775.  
  1776.             new_ypos = -1;
  1777.             menu_item = 0;
  1778.             menuname[0] = '\0';
  1779.             for (menu_item = 0; mp; menu_item++, mp = mp->next)
  1780.                 if (mp->position >= pagetop && mp->position < pagebot)
  1781.                 {
  1782.                     new_ypos = mp->position;
  1783.                     strcpy(menuname, mp->identifier);
  1784.                     break;
  1785.                 }
  1786.             if (new_ypos == -1)
  1787.                 menu_item = -1;
  1788.         }
  1789.  
  1790.         inhibit_display = window_bashed = 0;
  1791.         last_pagetop = pagetop;
  1792.  
  1793.         nodename[0] = '\0';    /* Don't display old text in input line. */
  1794.  
  1795.         if (new_ypos >= 0)
  1796.         {
  1797.             if (cursor_to(new_ypos) && menu_item >= 0)
  1798.             {
  1799.                 if (terminal_inverse_begin)
  1800.                     do_term (terminal_inverse_begin);
  1801.                 print_string ("%s", menuname);
  1802.                 if (terminal_inverse_begin)
  1803.                     do_term (terminal_end_attributes);
  1804.                 fflush(stdout);
  1805.             }
  1806.         }
  1807.         last_command = command;
  1808.         if (last_command == 'S')
  1809.             cursor_to(search_start);
  1810.         else
  1811.             goto_xy (echo_area.left, echo_area.top);
  1812.  
  1813.         command = blink_cursor ();
  1814.         clear_echo_area ();
  1815.  
  1816.         if (command == EOF)
  1817.         {
  1818.             done = 1;
  1819.             continue;
  1820.         }
  1821.         command = to_upper (command);
  1822.  
  1823.         switch (command)
  1824.         {
  1825.         case KUpArrow:
  1826.         case KBackTab:
  1827.             inhibit_display = 1;
  1828.             if (menu_item >= 0 && cursor_to(new_ypos))
  1829.             {
  1830.                 print_string ("%s", menuname);
  1831.                 cursor_to(new_ypos);
  1832.             }
  1833.             if (the_menu_size)
  1834.             {
  1835.                   COMP_ENTRY *mp = completion_list;
  1836.                 int i;
  1837.  
  1838.                 if (--menu_item < 0)
  1839.                     menu_item = 0;
  1840.                 for (i = 0; mp; i++, mp = mp->next)
  1841.                     if (i == menu_item)
  1842.                     {
  1843.                         new_ypos = mp->position;
  1844.                         strcpy(menuname, mp->identifier);
  1845.                         break;
  1846.                     }
  1847.                 if (new_ypos < pagetop)
  1848.                 {
  1849.                     bash_window_to(new_ypos);
  1850.                     inhibit_display = 0;
  1851.                 }
  1852.             }
  1853.             break;
  1854.  
  1855.         case KDownArrow:
  1856.         case KTab:
  1857.             inhibit_display = 1;
  1858.             if (menu_item >= 0 && cursor_to(new_ypos))
  1859.             {
  1860.                 print_string ("%s", menuname);
  1861.                 cursor_to(new_ypos);
  1862.             }
  1863.             if (the_menu_size)
  1864.             {
  1865.                   COMP_ENTRY *mp = completion_list;
  1866.                 int i;
  1867.  
  1868.                 if (++menu_item >= the_menu_size)
  1869.                     menu_item = the_menu_size - 1;
  1870.                 for (i = 0; mp; i++, mp = mp->next)
  1871.                     if (i == menu_item)
  1872.                     {
  1873.                         new_ypos = mp->position;
  1874.                         strcpy(menuname, mp->identifier);
  1875.                         break;
  1876.                     }
  1877.                 if (new_ypos >= pagebot)
  1878.                 {
  1879.                     bash_window_to(new_ypos);
  1880.                     inhibit_display = 0;
  1881.                 }
  1882.             }
  1883.             break;
  1884.  
  1885.         case KEnter:
  1886.         {
  1887.               COMP_ENTRY *mp = completion_list;
  1888.  
  1889.             for (; mp; mp = mp->next)
  1890.                 if (mp->position == new_ypos)
  1891.                 {
  1892.                     if (get_node ((char *) NULL, mp->data, 0))
  1893.                     {
  1894.                         clear_echo_area ();
  1895.                         new_node = 1;
  1896.                     }
  1897.                     break;
  1898.                 }
  1899.             if (!mp)
  1900.             {
  1901.                 inhibit_display =1;
  1902.                 display_error ("No menu items are available!");
  1903.             }
  1904.             break;
  1905.         }
  1906.  
  1907.         case KCtrlHome:
  1908.         case 'D':
  1909.             get_node ((char *) NULL, "(dir)Top", 0);
  1910.             new_node = 1;
  1911.             break;
  1912.  
  1913.         case 'H':
  1914.             if ((the_window.bottom - the_window.top) < 24)
  1915.                 get_node ((char *) NULL, "(info.info)Help-Small-Screen", 0);
  1916.             else
  1917.                 get_node ((char *) NULL, "(info.info)Help", 0);
  1918.             new_node = 1;
  1919.             break;
  1920.  
  1921.         case 'N':
  1922.         case KRightArrow:
  1923.             if (!next_node ())
  1924.             {
  1925.                 display_error ("No NEXT for this node!");
  1926.                 inhibit_display = 1;
  1927.             }
  1928.             else
  1929.                 new_node = 1;
  1930.             break;
  1931.  
  1932.         case KLeftArrow:
  1933.         case 'P':
  1934.             if (!prev_node ())
  1935.             {
  1936.                 display_error ("No PREV for this node!");
  1937.                 inhibit_display = 1;
  1938.             }
  1939.             else
  1940.                 new_node = 1;
  1941.             break;
  1942.  
  1943.         case 'U':
  1944.         case KCtrlUp:
  1945.         {
  1946.             int savetop = pagetop;
  1947.  
  1948.             if (!up_node ())
  1949.             {
  1950.                 display_error ("No UP for this node!");
  1951.                 inhibit_display = 1;
  1952.                 pagetop = savetop;
  1953.             }
  1954.             else
  1955.                 new_node = 1;
  1956.             break;
  1957.         }
  1958.  
  1959.         case 'M':
  1960.             if (!the_menu_size)
  1961.             {
  1962.                 display_error ("No menu in this node!");
  1963.                 inhibit_display = 1;
  1964.                 break;
  1965.             }
  1966.  
  1967.             if (!readline ("Menu item: ", nodename, NODENAME_LEN, 1))
  1968.             {
  1969.                 clear_echo_area ();
  1970.                 inhibit_display = 1;
  1971.                 break;
  1972.             }
  1973.  
  1974.             I_goto_xy (echo_area.left, echo_area.top);
  1975.             if (!find_menu_node (nodename, nodename))
  1976.             {
  1977.                 display_error ("\"%s\" is not a menu item!", nodename);
  1978.                 inhibit_display = 1;
  1979.                 break;
  1980.             }
  1981.  
  1982.             if (get_node ((char *) NULL, nodename, 0))
  1983.             {
  1984.                 clear_echo_area ();
  1985.                 new_node = 1;
  1986.             }
  1987.             break;
  1988.  
  1989.         case 'F':
  1990.         case KCtrlDown:
  1991.         {
  1992.             char footnote[NODENAME_LEN];
  1993.             if (!build_notes ())
  1994.             {
  1995.                 display_error ("No cross-references in this node!");
  1996.                 inhibit_display = 1;
  1997.                 break;
  1998.             }
  1999.             strcpy (footnote, visible_footnote);
  2000.             if (!readline ("Follow reference: ", footnote, NODENAME_LEN, 1))
  2001.             {
  2002.                 inhibit_display = 1;
  2003.                 build_menu();
  2004.                 break;
  2005.             }
  2006.  
  2007.             I_goto_xy (echo_area.left, echo_area.top);
  2008.             if (!find_note_node (footnote, nodename))
  2009.             {
  2010.                 display_error ("\"%s\" is not a cross-reference in this node!",
  2011.                         footnote);
  2012.                 inhibit_display = 1;
  2013.                 build_menu();
  2014.                 break;
  2015.             }
  2016.  
  2017.             if (get_node ((char *)NULL, nodename, 0))
  2018.             {
  2019.                 clear_echo_area ();
  2020.                 new_node = 1;
  2021.             }
  2022.             break;
  2023.         }
  2024.  
  2025.         case KBackSpace:
  2026.         case 'L':
  2027.         {
  2028.             char filename[FILENAME_LEN], nodename[NODENAME_LEN];
  2029.             int ptop, ntop;
  2030.             if (pop_node (filename, nodename, &ntop, &ptop) &&
  2031.                 get_node (filename, nodename, 1))
  2032.             {
  2033.                 pagetop = ptop;
  2034.                 new_node = 1;
  2035.             }
  2036.             else
  2037.                 inhibit_display = 1;
  2038.             break;
  2039.         }
  2040.  
  2041.         case SPACE:
  2042.         case CTRL ('V'):
  2043.         case KPgDn:
  2044.             if (!next_page ())
  2045.             {
  2046.         /*        display_error ("At last page of this node now!"); */
  2047.                 inhibit_display = 1;
  2048.             }
  2049.             break;
  2050.  
  2051.         case DELETE:
  2052.         case KPgUp:
  2053.             if (!prev_page ())
  2054.             {
  2055.         /*        display_error ("At first page of this node now!"); */
  2056.                 inhibit_display = 1;
  2057.             }
  2058.             break;
  2059.  
  2060.         case KEnd:    /* Move to end of node */
  2061.             if (!end_page())
  2062.                 inhibit_display = 1;
  2063.             break;
  2064.  
  2065.         case 'B':
  2066.         case KHome:
  2067.             if (pagetop == nodetop)
  2068.             {
  2069.         /*        display_error ("Already at beginning of this node!"); */
  2070.                 inhibit_display = 1;
  2071.             }
  2072.             else
  2073.                 pagetop = nodetop;
  2074.             break;
  2075.  
  2076.         /* I don't want to do this this way, but the documentation
  2077.             clearly states that '6' doesn't work.  It states this for a
  2078.             reason, and ours is not to wonder why... */
  2079.         case '1':
  2080.         case '2':
  2081.         case '3':
  2082.         case '4':
  2083.         case '5':
  2084.         {
  2085.             int item = command - '0';
  2086.  
  2087.             if (!the_menu_size)
  2088.             {
  2089.                 display_error ("No menu in this node!");
  2090.                 inhibit_display = 1;
  2091.                 break;
  2092.             }
  2093.  
  2094.             if (item > the_menu_size)
  2095.             {
  2096.                 display_error ("There are only %d items in the menu!",
  2097.                         the_menu_size);
  2098.                 inhibit_display = 1;
  2099.                 break;
  2100.             }
  2101.  
  2102.             if (!get_menu (item))
  2103.                 inhibit_display = 1;
  2104.             else
  2105.                 new_node = 1;
  2106.             break;
  2107.         }
  2108.  
  2109.         case 'G':
  2110.             if (!readline ("Goto node: ", nodename, NODENAME_LEN, 0))
  2111.             {
  2112.                 inhibit_display = 1;
  2113.                 break;
  2114.             }
  2115.  
  2116.             if (get_node ((char *) NULL, nodename, 0))
  2117.             {
  2118.                 clear_echo_area ();
  2119.                 new_node = 1;
  2120.             }
  2121.             break;
  2122.  
  2123.         /* Search from the starting position forward for a string.
  2124.             Select the node containing the desired string.  Put the
  2125.             top of the page screen_lines / 2 lines behind it, but not
  2126.             before nodetop. */
  2127.         case 'S':
  2128.         {
  2129.             extern int info_buffer_len;
  2130.             int pointer, temp;
  2131.              char prompt[21 + NODENAME_LEN + 1];
  2132.              static char search_string[NODENAME_LEN] = "";
  2133.              static char *starting_filename = NULL,
  2134.                     *starting_nodename = NULL;
  2135.             static int starting_pagetop = 0;
  2136.             static int wrap_search = 0;
  2137.  
  2138.             if (menu_item >= 0 && cursor_to(new_ypos))
  2139.             {
  2140.                 print_string ("%s", menuname);
  2141.                 cursor_to(new_ypos);
  2142.             }
  2143.              sprintf (prompt, "Search for string [%s]: ", search_string);
  2144.  
  2145.              if (!readline (prompt , nodename, NODENAME_LEN, 0))
  2146.             {
  2147.                 inhibit_display = 1;
  2148.                 break;
  2149.             }
  2150.  
  2151.             /* If the user defaulted the search string, and the previous
  2152.             command was search, then this is a continuation of the
  2153.             previous search. */
  2154.             if (((strcmp (nodename, search_string) == 0) ||
  2155.                 (!*nodename && *search_string)) &&
  2156.                 (last_command == 'S'))
  2157.             {
  2158.                 search_start++;
  2159.             }
  2160.             else
  2161.             {
  2162.                 /* Initialize the start of a new search. */
  2163.                 if (starting_filename)
  2164.                     free (starting_filename);
  2165.  
  2166.                 starting_filename = savestring (last_loaded_info_file);
  2167.  
  2168.                 if (starting_nodename)
  2169.                     free (starting_nodename);
  2170.  
  2171.                 starting_nodename = savestring (nodename ? nodename : "");
  2172.  
  2173.                 starting_pagetop = pagetop;
  2174.                 search_start = pagetop;
  2175.                 wrap_search = 0;
  2176.  
  2177.                 if (*nodename != '\0')
  2178.                     strcpy (search_string, nodename);
  2179.             }
  2180.  
  2181.             I_goto_xy (echo_area.left, echo_area.top);
  2182.  
  2183.             {
  2184.                 static int pushed = 0; /* How many files are pushed? */
  2185.                 int found_string = 0;  /* Did we find our string? */
  2186.  
  2187.                 if (wrap_search)
  2188.                 {
  2189.                     push_filestack (next_info_file ((char *)NULL, 1), 0);
  2190.                     pushed++;
  2191.                     search_start = 0;
  2192.                 }
  2193.  
  2194.                 for (;;)
  2195.                 {
  2196.                     set_search_constraints (info_file, info_buffer_len);
  2197.                     pointer = search_forward (search_string, search_start);
  2198.  
  2199.                     if (pointer != -1)
  2200.                     {
  2201.                         found_string = 1;
  2202.                         break;
  2203.                     }
  2204.                     else
  2205.                     {
  2206.                         char *next_file;
  2207.  
  2208.                         next_file = next_info_file ((char *)NULL, 0);
  2209.  
  2210.                         if (next_file != NULL)
  2211.                         {
  2212.                             if (pushed)
  2213.                             {
  2214.                                 pop_filestack ();
  2215.                                 pushed--;
  2216.                             }
  2217.  
  2218.                             push_filestack (next_file, 0);
  2219.                             pushed++;
  2220.                             search_start = 0;
  2221.                 #if 1
  2222.                             I_goto_xy (echo_area.left, echo_area.top);
  2223.                             print_string ("Searching file %s...\n", next_file);
  2224.                 #endif
  2225.                             continue;
  2226.                         }
  2227.  
  2228.                         if (wrap_search)
  2229.                         {
  2230.                             display_error
  2231.                                 ("\"%s\" not found!",
  2232.                                 search_string);
  2233.  
  2234.                             inhibit_display = 1;
  2235.                             wrap_search = 0;
  2236.  
  2237.                             if (pushed)
  2238.                             {
  2239.                                 pop_filestack ();
  2240.                                 pushed--;
  2241.                             }
  2242.                             break;
  2243.                         }
  2244.                         else
  2245.                         {
  2246.                             display_error ("Search: End of file");
  2247.                             inhibit_display = 1;
  2248.                             wrap_search = 1;
  2249.  
  2250.                             if (pushed)
  2251.                             {
  2252.                                 pop_filestack ();
  2253.                                 pushed--;
  2254.                             }
  2255.                             break;
  2256.                         }
  2257.                     }
  2258.                 }
  2259.  
  2260.                 if (pushed)
  2261.                 {
  2262.                     swap_filestack ();
  2263.                     pop_filestack ();
  2264.                     pushed--;
  2265.                 }
  2266.  
  2267.                 if (!found_string)
  2268.                     break;
  2269.  
  2270.                 wrap_search = 0;
  2271.                 temp = search_backward (start_of_node_string, pointer);
  2272.  
  2273.                 if (temp != -1)
  2274.                  {
  2275.                     search_start = pointer;
  2276.                     pointer = forward_lines (1, temp);
  2277.                 }
  2278.  
  2279.                 if (temp == -1 || !extract_field ("Node:", nodename, pointer))
  2280.                 {
  2281.                     display_error("%d: There doesn't appear to be a nodename for this node.", pointer);
  2282.         
  2283.                     get_node ((char *)NULL, "*", 0);
  2284.                     pagetop = pointer;
  2285.                     new_node = 1;
  2286.                     break;
  2287.                 }
  2288.      
  2289.                 /* Get the node if it is different than the one already
  2290.                 loaded. */
  2291.                 if (strcmp (nodename, starting_nodename) != 0)
  2292.                 {
  2293.                     free (starting_nodename);
  2294.                     starting_nodename = savestring (nodename);
  2295.  
  2296.                     if (get_node ((char *) NULL, nodename, 0))
  2297.                     {
  2298.                         clear_echo_area ();
  2299.                         new_node = 1;
  2300.                     }
  2301.                 }
  2302.      
  2303.                 /* Reset the top of page if necessary. */
  2304.                 {
  2305.                     if ((strcmp (last_loaded_info_file, starting_filename) != 0) ||
  2306.                         (starting_pagetop != pagetop) ||
  2307.                         (search_start > pagebot))
  2308.                         bash_window_to(search_start);
  2309.                     else
  2310.                         inhibit_display = 1;
  2311.                    }
  2312.                    break;
  2313.                }
  2314.            }
  2315.  
  2316.     /*    case CTRL ('H'):    */
  2317.         case KF1:
  2318.         case '?':
  2319.             get_node ((char *) NULL, "(dir)HelpKeys", 0);
  2320.             break;
  2321.  
  2322.         case KEsc:
  2323.         case 'Q':
  2324.         case 'X':
  2325.             done = 1;
  2326.             break;
  2327.  
  2328.         case CTRL ('L'):    /* Control-l is redisplay. */
  2329.             window_bashed = 1;
  2330.             if (last_command == 'S')
  2331.                 command = 'S';
  2332.             break;
  2333.  
  2334.         case '(':    /* You *must* be trying to type a complete nodename. */
  2335.             strcpy (nodename, "(");
  2336.             if (!readline ("Goto node: ", nodename, NODENAME_LEN, 0))
  2337.             {
  2338.                 inhibit_display = 1;
  2339.                 clear_echo_area ();
  2340.                 break;
  2341.             }
  2342.             I_goto_xy (echo_area.left, echo_area.top);
  2343.             if (get_node ((char *) NULL, nodename, 0))
  2344.                 clear_echo_area ();
  2345.             break;
  2346.  
  2347.         case CTRL ('P'):
  2348.         /* Print the contents of this node on the default printer.  We
  2349.             would like to let the user specify the printer, but we don't
  2350.             want to ask her each time which printer to use.  Besides, he
  2351.             might not know, which is why it (the user) is in the need of
  2352.             Info. */
  2353.         {
  2354.             char *tempname = make_temp_filename (current_info_node);
  2355.             if (dump_current_node (tempname) == 0 &&
  2356.                 printfile (tempname) == 0 &&
  2357.                 deletefile (tempname) == 0)
  2358.             {
  2359.                 display_error ("Printed node.  Go pick up your output.\n");
  2360.             }
  2361.             inhibit_display = 1;
  2362.             free (tempname);
  2363.         }
  2364.         break;
  2365.  
  2366.         default:
  2367.             inhibit_display = 1;
  2368.             display_error ("Unknown command! Press '?' for help.");
  2369.         }
  2370.     }
  2371. }
  2372.  
  2373. /* Return the screen column width that the line from START to END
  2374.    requires to display. */
  2375. line_length (start, end)
  2376.      int start, end;
  2377. {
  2378.   int count = 0;
  2379.  
  2380.   while (start < end)
  2381.     {
  2382.       if (info_file[start] == '\t')
  2383.     count += 7 - (count % 8);
  2384.       else if (CTRL_P (info_file[start]))
  2385.     count += 2;
  2386.       else
  2387.     count++;
  2388.  
  2389.       start++;
  2390.     }
  2391.  
  2392.   return (count);
  2393. }
  2394.  
  2395. /* Move to the node specified in the NEXT field. */
  2396. int
  2397. next_node ()
  2398. {
  2399.   char nodename[NODENAME_LEN];
  2400.  
  2401.   if (!extract_field ("Next:", nodename, nodetop))
  2402.     return (0);
  2403.   return (get_node ((char *) NULL, nodename, 0));
  2404. }
  2405.  
  2406. /* Move to the node specified in the PREVIOUS field. */
  2407. int
  2408. prev_node ()
  2409. {
  2410.   char nodename[NODENAME_LEN];
  2411.  
  2412.   if (!extract_field ("Previous:", nodename, nodetop)
  2413.       && !extract_field ("Prev:", nodename, nodetop))
  2414.     return (0);
  2415.   return (get_node ((char *) NULL, nodename, 0));
  2416. }
  2417.  
  2418. /* Move to the node specified in the UP field. */
  2419. int
  2420. up_node ()
  2421. {
  2422.   char nodename[NODENAME_LEN];
  2423.  
  2424.   if (!extract_field ("Up:", nodename, nodetop))
  2425.     return (0);
  2426.   return (get_node ((char *) NULL, nodename, 0));
  2427. }
  2428.  
  2429. /* Build a completion list of menuname/nodename for each
  2430.    line in this node that is a menu item. */
  2431. int
  2432. build_menu ()
  2433. {
  2434.   int pointer = nodetop;
  2435.   char menuname[NODENAME_LEN];
  2436.   char nodename[NODENAME_LEN];
  2437.  
  2438.   if (strcmp (menus_nodename, current_info_node) == 0 &&
  2439.       strcmp (menus_filename, current_info_file) == 0)
  2440.     return (the_menu_size != 0);
  2441.  
  2442.   strcpy (menus_nodename, current_info_node);
  2443.   strcpy (menus_filename, current_info_file);
  2444.   free_completion_list ();
  2445.   the_menu_size = 0;
  2446.  
  2447.   set_search_constraints (info_file, nodebot);
  2448.   if ((pointer = search_forward (MENU_HEADER, nodetop)) < 0)
  2449.     return (0);
  2450.  
  2451.   /* There is a menu here.  Look for members of it. */
  2452.   pointer += strlen (MENU_HEADER);
  2453.  
  2454.   while (1)
  2455.     {
  2456.       int idx, menu_pointer;
  2457.  
  2458.       pointer = search_forward (MENU_ID, pointer);
  2459.       if (pointer < 0)
  2460.     break;            /* No more menus in this node. */
  2461.  
  2462.       menu_pointer =
  2463.         pointer = (skip_whitespace (pointer + strlen (MENU_ID)));
  2464.  
  2465.  
  2466.       idx = 0;
  2467.       while ((menuname[idx] = info_file[pointer]) && menuname[idx] != ':')
  2468.     idx++, pointer++;
  2469.  
  2470.       menuname[idx] = '\0';
  2471.       pointer++;
  2472.  
  2473.       if (info_file[pointer] == ':')
  2474.     {
  2475.       strcpy (nodename, menuname);
  2476.     }
  2477.       else
  2478.     {
  2479.       int in_parens;
  2480.  
  2481.       pointer = skip_whitespace (pointer);
  2482.       idx = in_parens = 0;
  2483.  
  2484.       while ((nodename[idx] = info_file[pointer]) &&
  2485.          (in_parens ||
  2486.           (nodename[idx] != '\t' &&
  2487.            nodename[idx] != '.' &&
  2488.            nodename[idx] != ',')))
  2489.         {
  2490.           if (nodename[idx] == '(')
  2491.         in_parens++;
  2492.           else if (nodename[idx] == ')')
  2493.         in_parens--;
  2494.  
  2495.           idx++, pointer++;
  2496.         }
  2497.       nodename[idx] = '\0';
  2498.     }
  2499.       add_completion (menuname, nodename, menu_pointer);
  2500.       the_menu_size++;
  2501.     }
  2502.   if (the_menu_size)
  2503.     completion_list = reverse_list (completion_list);
  2504.   return (the_menu_size != 0);
  2505. }
  2506.  
  2507. /* Select ITEMth item from a list built by build_menu (). */
  2508. int
  2509. get_menu (item)
  2510.      int item;
  2511. {
  2512.   if (!build_menu ())
  2513.     return (0);
  2514.  
  2515.   if (item > the_menu_size)
  2516.     return (0);
  2517.   else
  2518.     {
  2519.       COMP_ENTRY *temp = completion_list;
  2520.  
  2521.       while (--item && temp)
  2522.     temp = temp->next;
  2523.  
  2524.       return (get_node ((char *) NULL, temp->data, 0));
  2525.     }
  2526. }
  2527.  
  2528. /* Scan through the ?already? built menu list looking
  2529.    for STRING.  If you find it, put the corresponding nodes
  2530.    name in NODENAME. */
  2531. int
  2532. find_menu_node (string, nodename)
  2533.      char *string, *nodename;
  2534. {
  2535.   return (scan_list (string, nodename));
  2536. }
  2537.  
  2538. /* The work part of find_menu_node and find_note_node. */
  2539. int
  2540. scan_list (string, nodename)
  2541.      char *string, *nodename;
  2542. {
  2543.   COMP_ENTRY *temp = completion_list;
  2544.  
  2545.   while (temp)
  2546.     {
  2547.       if (strnicmp (string, temp->identifier, strlen (string)) == 0)
  2548.     {
  2549.       strcpy (nodename, temp->data);
  2550.       return (1);
  2551.     }
  2552.       temp = temp->next;
  2553.     }
  2554.   return (0);
  2555. }
  2556.  
  2557. /* Remove <CR> and whitespace from string, replacing them with
  2558.    only one space.  Exception:  <CR> at end of string disappears. */
  2559. clean_up (string)
  2560.      char *string;
  2561. {
  2562.   char *to;
  2563.  
  2564.   /* Skip all whitespace characters found at the start of STRING. */
  2565.   while (whitespace (*string))
  2566.     string++;
  2567.  
  2568.   to = string;
  2569.  
  2570.   while (*to = *string++)
  2571.     {
  2572.       if (*to == '\n' || *to == ' ')
  2573.     {
  2574.       *to = ' ';
  2575.  
  2576.       while (*string == ' ' || *string == '\t')
  2577.         string++;
  2578.     }
  2579.     to++;
  2580.     }
  2581. }
  2582.  
  2583. /* Find a reference to "*Note".  Return the offset of the start
  2584.    of that reference, or -1. */
  2585. find_footnote_ref (from)
  2586.      int from;
  2587. {
  2588.   while (1)
  2589.     {
  2590.       from = search_forward (FOOTNOTE_HEADER, from);
  2591.       if (from < 0)
  2592.     return (from);
  2593.       else
  2594.     from += strlen (FOOTNOTE_HEADER);
  2595.       if (info_file[from] == ' ' ||
  2596.       info_file[from] == '\n' ||
  2597.       info_file[from] == '\t')
  2598.     return (from);
  2599.     }
  2600. }
  2601.  
  2602. /* Build an array of (footnote.nodename) for each footnote in this node. */
  2603. int
  2604. build_notes ()
  2605. {
  2606.   int pointer;
  2607.   char notename[NODENAME_LEN];
  2608.   char nodename[NODENAME_LEN];
  2609.  
  2610.   set_search_constraints (info_file, nodebot);
  2611.  
  2612.   if ((find_footnote_ref (nodetop)) < 0)
  2613.     return (0);
  2614.   pointer = nodetop;
  2615.  
  2616.   menus_filename[0] = menus_nodename[0] = '\0';
  2617.   visible_footnote = "";
  2618.   free_completion_list ();
  2619.  
  2620.   while (1)
  2621.     {
  2622.       int idx, menu_pointer;
  2623.  
  2624.       pointer = find_footnote_ref (pointer);
  2625.       if (pointer < 0)
  2626.     break;            /* no more footnotes in this node. */
  2627.  
  2628.       menu_pointer = 
  2629.         pointer = skip_whitespace_and_cr (pointer);
  2630.       idx = 0;
  2631.  
  2632.       while ((notename[idx] = info_file[pointer]) && notename[idx] != ':')
  2633.     {
  2634.       idx++, pointer++;
  2635.     }
  2636.  
  2637.       notename[idx] = '\0';
  2638.       clean_up (notename);
  2639.       pointer++;
  2640.       if (info_file[pointer] == ':')
  2641.     {
  2642.       strcpy (nodename, notename);
  2643.     }
  2644.       else
  2645.     {
  2646.       int in_parens = 0;
  2647.  
  2648.       pointer = skip_whitespace (pointer);
  2649.       idx = 0;
  2650.  
  2651.       while ((nodename[idx] = info_file[pointer]) &&
  2652.          (in_parens ||
  2653.           (nodename[idx] != '\t' &&
  2654.            nodename[idx] != '.' &&
  2655.            nodename[idx] != ',')))
  2656.         {
  2657.           if (nodename[idx] == '(')
  2658.         in_parens++;
  2659.           else if (nodename[idx] == ')')
  2660.         in_parens--;
  2661.  
  2662.           idx++, pointer++;
  2663.         }
  2664.       nodename[idx] = '\0';
  2665.       clean_up (nodename);
  2666.     }
  2667.       /* Add the notename/nodename to the list. */
  2668.       add_completion (notename, nodename, menu_pointer);
  2669.       the_menu_size++;
  2670.  
  2671.       /* Remember this identifier as the default if it is the first one in the
  2672.          page. */
  2673.       if (!(*visible_footnote) &&
  2674.       pointer > pagetop &&
  2675.       pointer < forward_lines (the_window.bottom - the_window.top, pointer))
  2676.     visible_footnote = completion_list->identifier;
  2677.     }
  2678.   if (the_menu_size)
  2679.     completion_list = reverse_list (completion_list);
  2680.   return (the_menu_size != 0);
  2681. }
  2682.  
  2683. /* Scan through the ?already? built footnote list looking for STRING.
  2684.    If found, place the corresponding node name in NODENAME. */
  2685. int
  2686. find_note_node (string, nodename)
  2687.      char *string, *nodename;
  2688. {
  2689.   return (scan_list (string, nodename));
  2690. }
  2691.  
  2692. /* **************************************************************** */
  2693. /*                                    */
  2694. /*            Page Display                     */
  2695. /*                                    */
  2696. /* **************************************************************** */
  2697.  
  2698.  
  2699. /* The display functions for GNU Info. */
  2700. int display_ch, display_cv;
  2701. int display_point;
  2702.  
  2703. /* Display the current page from pagetop down to the bottom of the
  2704.    page or the bottom of the node, whichever comes first. */
  2705. display_page ()
  2706. {
  2707.   display_point = pagetop;
  2708.   display_ch = the_window.left;
  2709.   display_cv = the_window.top;
  2710.   generic_page_display ();
  2711. }
  2712.  
  2713. /* Print the page from display_point to bottom of node, or window,
  2714.    whichever comes first.  Start printing at display_ch, display_cv. */
  2715. generic_page_display ()
  2716. {
  2717.   int done_with_display = 0;
  2718.   int character;
  2719.  
  2720.   goto_xy (display_ch, display_cv);
  2721.  
  2722.   while (!done_with_display)
  2723.     {
  2724.       if (display_point == nodebot)
  2725.     {
  2726.       clear_eop ();
  2727.       goto display_finish;
  2728.     }
  2729.  
  2730.       character = info_file[display_point];
  2731.  
  2732.       if ((display_width (character, the_window.ch) + the_window.ch)
  2733.       >= the_window.right)
  2734.     display_carefully (character);
  2735.       else
  2736.     charout (character);
  2737.  
  2738.       if ((the_window.cv >= the_window.bottom)
  2739.       || (the_window.cv == the_window.top
  2740.           && the_window.ch == the_window.left))
  2741.     {
  2742.     display_finish:
  2743.       pagebot = display_point;
  2744.       make_modeline ();
  2745.       done_with_display++;
  2746.       continue;
  2747.     }
  2748.       else
  2749.     display_point++;
  2750.     }
  2751.   fflush (stdout);
  2752. }
  2753.  
  2754. /* Display character carefully, ensuring that no scrolling takes
  2755.    place, even in the case of funky control characters. */
  2756. display_carefully (character)
  2757.      int character;
  2758. {
  2759.   if (CTRL_P (character))
  2760.     {
  2761.       switch (character)
  2762.     {
  2763.     case RETURN:
  2764.     case NEWLINE:
  2765.     case TAB:
  2766.       clear_eol ();
  2767.       advance (the_window.right - the_window.ch);
  2768.       break;
  2769.     default:
  2770. /*      charout ('^');
  2771.       if (the_window.cv == the_window.bottom)
  2772.         break;
  2773.       else
  2774.         charout (UNCTRL (character));    */
  2775.         break;
  2776.     }
  2777.     }
  2778.   else
  2779.     charout (character);
  2780. }
  2781.  
  2782. /* Move the cursor to POSITION in page.  Return non-zero if successful. */
  2783. cursor_to (position)
  2784.      int position;
  2785. {
  2786.   int ch, cv, character;
  2787.   int point;
  2788.  
  2789.   if (position > pagebot || position < pagetop)
  2790.     return (0);
  2791.  
  2792.   point = pagetop;
  2793.   ch = the_window.left;
  2794.   cv = the_window.top;
  2795.  
  2796.   while (point < position)
  2797.     {
  2798.       character = info_file[point++];
  2799.  
  2800.       ch += display_width (character, ch);
  2801.  
  2802.       if (ch >= the_window.right)
  2803.     {
  2804.       ch = ch - the_window.right;
  2805.       cv++;
  2806.  
  2807.       if (cv >= the_window.bottom)
  2808.         return (0);
  2809.     }
  2810.     }
  2811.   goto_xy (ch, cv);
  2812.   return (1);
  2813. }
  2814.  
  2815. /* Move to the next page in this node.  Return 0 if
  2816.    we can't get to the next page. */
  2817. int
  2818. next_page ()
  2819. {
  2820.   int pointer;
  2821.  
  2822.   pointer =
  2823.     forward_lines ((the_window.bottom - the_window.top) - 2, pagetop);
  2824.  
  2825.   if (pointer >= nodebot)
  2826.     return (0);
  2827.  
  2828.   /* Hack for screens smaller than displayed line width. */
  2829.   if (pointer > display_point)
  2830.     {
  2831.       pointer = display_point;
  2832.       back_lines (1);
  2833.     }
  2834.   pagetop = pointer;
  2835.   return (1);
  2836. }
  2837.  
  2838. /* Move to the end of this node.  Return 0 if we are already there */
  2839. int end_page ()
  2840. {
  2841.   int pointer;
  2842.  
  2843.   pointer = back_lines (the_window.bottom - the_window.top - 2, nodebot);
  2844.   if (pointer < pagetop)
  2845.     return (0);
  2846.   pagetop = pointer;
  2847.   return (1);
  2848. }
  2849.  
  2850. /* Move to the previous page in this node.  Return zero if
  2851.    there is no previous page. */
  2852. int
  2853. prev_page ()
  2854. {
  2855.   int pointer =
  2856.   back_lines ((the_window.bottom - the_window.top) - 2, pagetop);
  2857.  
  2858.   if (pagetop == nodetop)
  2859.     return (0);
  2860.  
  2861.   if (pointer < nodetop)
  2862.     pointer = nodetop;
  2863.  
  2864.   pagetop = pointer;
  2865.   return (1);
  2866. }
  2867.  
  2868.  
  2869. /* **************************************************************** */
  2870. /*                                    */
  2871. /*            Utility Functions                */
  2872. /*                                    */
  2873. /* **************************************************************** */
  2874.  
  2875. char *search_buffer;        /* area in ram to scan through. */
  2876. int buffer_bottom;        /* Length of this area. */
  2877.  
  2878. /* Set the global variables that all of these routines use. */
  2879. set_search_constraints (buffer, extent)
  2880.      char *buffer;
  2881.      int extent;
  2882. {
  2883.   search_buffer = buffer;
  2884.   buffer_bottom = extent;
  2885. }
  2886.  
  2887. /* Move back to the start of this line. */
  2888. to_beg_line (from)
  2889.      int from;
  2890. {
  2891.   while (from && search_buffer[from - 1] != '\n')
  2892.     from--;
  2893.   return (from);
  2894. }
  2895.  
  2896. /* Move forward to the end of this line. */
  2897. to_end_line (from)
  2898. {
  2899.   while (from < buffer_bottom && search_buffer[from] != '\n')
  2900.     from++;
  2901.   return (from);
  2902. }
  2903.  
  2904. /* Move back count lines in search_buffer starting at starting_pos.
  2905.    Returns the start of that line. */
  2906. back_lines (count, starting_pos)
  2907.      int count, starting_pos;
  2908. {
  2909.   starting_pos = to_beg_line (starting_pos);
  2910.   while (starting_pos && count)
  2911.     {
  2912.       starting_pos = to_beg_line (starting_pos - 1);
  2913.       count--;
  2914.     }
  2915.   return (starting_pos);
  2916. }
  2917.  
  2918. /* Move forward count lines starting at starting_pos.
  2919.    Returns the start of that line. */
  2920. forward_lines (count, starting_pos)
  2921.      int count, starting_pos;
  2922. {
  2923.   starting_pos = to_end_line (starting_pos);
  2924.   while (starting_pos < buffer_bottom && count)
  2925.     {
  2926.       starting_pos = to_end_line (starting_pos + 1);
  2927.       count--;
  2928.     }
  2929.   return (to_beg_line (starting_pos));
  2930. }
  2931.  
  2932. /* Search for STRING in SEARCH_BUFFER starting at STARTING_POS.
  2933.    Return the location of the string, or -1 if not found. */
  2934. search_forward (string, starting_pos)
  2935.      char *string;
  2936.      int starting_pos;
  2937. {
  2938.   register int c, i, len;
  2939.   register char *buff, *end;
  2940.   char *alternate;
  2941.  
  2942.  
  2943.   /* We match characters in SEARCH_BUFFER against STRING and ALTERNATE.
  2944.      ALTERNATE is a case reversed version of STRING; this is cheaper than
  2945.      case folding each character before comparison. */
  2946.  
  2947.   /* Build the alternate string. */
  2948.   alternate = savestring (string);
  2949.   len = strlen (string);
  2950.  
  2951.   for (i = 0; i < len; i++)
  2952.     {
  2953.       c = alternate[i];
  2954.  
  2955.       if (c >= 'a' && c <= 'z')
  2956.     alternate[i] = c - 32;
  2957.       else if (c >= 'A' && c <= 'Z')
  2958.     alternate[i] = c + 32;
  2959.     }
  2960.  
  2961.   buff = search_buffer + starting_pos;
  2962.   end = search_buffer + buffer_bottom + 1;
  2963.  
  2964.   while (buff < end)
  2965.     {
  2966.       for (i = 0; i < len; i++)
  2967.     {
  2968.       c = buff[i];
  2969.  
  2970.       if (c != string[i] && c != alternate[i])
  2971.         break;
  2972.     }
  2973.  
  2974.       if (!string[i])
  2975.     {
  2976.       free (alternate);
  2977.       return (buff - search_buffer);
  2978.     }
  2979.  
  2980.       buff++;
  2981.     }
  2982.  
  2983.   free (alternate);
  2984.   return (-1);
  2985. }
  2986.  
  2987. /* Search for STRING in SEARCH_BUFFER starting at STARTING_POS.
  2988.    Return the location of the string, or -1 if not found. */
  2989. search_backward (string, starting_pos)
  2990.      char *string;
  2991.      int starting_pos;
  2992. {
  2993.   int len = strlen (string);
  2994.   while (starting_pos - len > -1)
  2995.     {
  2996.       if (strnicmp (search_buffer + (starting_pos - len), string, len) == 0)
  2997.     return (starting_pos - len);
  2998.       else
  2999.     starting_pos--;
  3000.     }
  3001.   return (-1);
  3002. }
  3003.  
  3004. /* Only search for STRING from POINTER to end of line.  Return offset
  3005.    of string, or -1 if not found. */
  3006. string_in_line (string, pointer)
  3007.      char *string;
  3008.      int pointer;
  3009. {
  3010.   int old_buffer_bottom = buffer_bottom;
  3011.  
  3012.   set_search_constraints (search_buffer, to_end_line (pointer));
  3013.   pointer = search_forward (string, pointer);
  3014.   buffer_bottom = old_buffer_bottom;
  3015.   return (pointer);
  3016. }
  3017.  
  3018. /* Skip whitespace characters at OFFSET in SEARCH_BUFFER.
  3019.    Return the next non-whitespace character or -1 if BUFFER_BOTTOM
  3020.    is reached. */
  3021. skip_whitespace (offset)
  3022.      int offset;
  3023. {
  3024.   int character;
  3025.  
  3026.   while (offset < buffer_bottom)
  3027.     {
  3028.       character = search_buffer[offset];
  3029.       if (character == ' ' || character == '\t')
  3030.     offset++;
  3031.       else
  3032.     return (offset);
  3033.     }
  3034.   return (-1);
  3035. }
  3036.  
  3037. /* Skip whitespace characters including <CR> at OFFSET in
  3038.    SEARCH_BUFFER.  Return the position of the next non-whitespace
  3039.    character, or -1 if BUFFER_BOTTOM is reached. */
  3040. skip_whitespace_and_cr (offset)
  3041.      int offset;
  3042. {
  3043.   while (1)
  3044.     {
  3045.       offset = skip_whitespace (offset);
  3046.       if (offset > 0 && search_buffer[offset] != '\n')
  3047.     return (offset);
  3048.       else
  3049.     offset++;
  3050.     }
  3051. }
  3052.  
  3053. /* Extract the node name part of the of the text after the FIELD.
  3054.    Place the node name into NODENAME.  Assume the line starts at
  3055.    OFFSET in SEARCH_BUFFER. */
  3056. int
  3057. extract_field (field_name, nodename, offset)
  3058.      char *field_name, *nodename;
  3059.      int offset;
  3060. {
  3061.   int temp, character;
  3062.   char * org_node_name = nodename;
  3063.  
  3064.   temp = string_in_line (field_name, offset);
  3065.   if (temp < 0)
  3066.     return (0);
  3067.  
  3068.   temp += strlen (field_name);
  3069.   temp = skip_whitespace (temp);
  3070.  
  3071.   /* Okay, place the following text into NODENAME. */
  3072.  
  3073.   while ((character = search_buffer[temp]) != ','
  3074.      && character != '\n' && character != '\r'
  3075.      && character != '\t')
  3076.     {
  3077.       *nodename = character;
  3078.       nodename++;
  3079.       temp++;
  3080.     }
  3081.   *nodename = '\0';
  3082.   return (1);
  3083. }
  3084.  
  3085. /* Return non-zero if pointer is exactly at string, else zero. */
  3086. int
  3087. looking_at (string, pointer)
  3088.      char *string;
  3089.      int pointer;
  3090. {
  3091.   if (strnicmp (search_buffer + pointer, string, strlen (string)) == 0)
  3092.     return (1);
  3093.   else
  3094.     return (0);
  3095. }
  3096.  
  3097. /* File stack stuff. This is currently only used to push one file while
  3098.    searching indirect files, but we may as well write it in full
  3099.    generality. */
  3100. typedef struct filestack
  3101. {
  3102.   struct filestack *next;
  3103.   char filename[FILENAME_LEN];
  3104.   char current_filename[FILENAME_LEN];
  3105.   char *tag_table;
  3106.   char *info_file;
  3107.   int info_buffer_len;
  3108. } FILESTACK;
  3109.  
  3110. FILESTACK *filestack = NULL;
  3111.  
  3112. int
  3113. push_filestack (filename, remember_name)
  3114.      char *filename;
  3115.      int remember_name;
  3116. {
  3117.   FILESTACK *element = (FILESTACK *) xmalloc (sizeof (FILESTACK));
  3118.  
  3119.   element->next = filestack;
  3120.   filestack = element;
  3121.  
  3122.   strcpy (filestack->filename, last_loaded_info_file);
  3123.   strcpy (filestack->current_filename, current_info_file);
  3124.   filestack->tag_table = tag_table;
  3125.   filestack->info_file = info_file;
  3126.   filestack->info_buffer_len = info_buffer_len;
  3127.   
  3128.   *last_loaded_info_file = '\0';    /* force the file to be read */
  3129.   if (get_info_file (filename, remember_name))
  3130.     {
  3131.       return (1);
  3132.     }
  3133.   else
  3134.     {
  3135.       pop_filestack ();
  3136.       return (0);
  3137.     }
  3138. }
  3139.  
  3140. void
  3141. pop_filestack ()
  3142. {
  3143.   FILESTACK *temp;
  3144.    
  3145.   if (filestack == NULL)
  3146.     {
  3147.        fprintf (stderr , "File stack is empty and can't be popped\n");
  3148.        brians_error ();
  3149.        return;
  3150.     }
  3151.  
  3152.   free (info_file);
  3153.  
  3154.   strcpy (last_loaded_info_file, filestack->filename);
  3155.   strcpy (current_info_file, filestack->current_filename);
  3156.   tag_table = filestack->tag_table;
  3157.   info_file = filestack->info_file;
  3158.   info_buffer_len = filestack->info_buffer_len;
  3159.  
  3160.   temp = filestack;
  3161.   filestack = filestack->next;
  3162.   free ((char *)temp);
  3163. }
  3164.  
  3165. /* Swap the current info file with the bottom of the filestack */
  3166. void
  3167. swap_filestack ()
  3168. {
  3169.   char t_last_loaded_info_file[FILENAME_LEN];
  3170.   char t_current_info_file[FILENAME_LEN];
  3171.   char *t_tag_table;
  3172.   char *t_info_file;
  3173.   int t_info_buffer_len;
  3174.  
  3175.   if (filestack == NULL)
  3176.     {
  3177.        fprintf (stderr , "File stack is empty and can't be swapped\n");
  3178.        brians_error ();
  3179.        return;
  3180.     }
  3181.  
  3182.   strcpy (t_last_loaded_info_file, filestack->filename);
  3183.   strcpy (t_current_info_file, filestack->current_filename);
  3184.   t_tag_table = filestack->tag_table;
  3185.   t_info_file = info_file;
  3186.   t_info_buffer_len = info_buffer_len;
  3187.  
  3188.   strcpy (filestack->filename, last_loaded_info_file);
  3189.   strcpy (filestack->current_filename, current_info_file);
  3190.   filestack->tag_table = tag_table;
  3191.   filestack->info_file = info_file;
  3192.   filestack->info_buffer_len = info_buffer_len;
  3193.  
  3194.   strcpy (last_loaded_info_file, t_last_loaded_info_file);
  3195.   strcpy (current_info_file, t_current_info_file);
  3196.   tag_table = t_tag_table;
  3197.   info_file = t_info_file;
  3198.   info_buffer_len = t_info_buffer_len;
  3199. }
  3200.  
  3201. /* Now the node history stack */
  3202.  
  3203. extern NODEINFO *Info_History;
  3204.  
  3205. /* Save the current filename, nodename, and position on the history list.
  3206.    We prepend. */
  3207. int
  3208. push_node (filename, nodename, page_position, node_position)
  3209.      char *filename, *nodename;
  3210.      int page_position, node_position;
  3211. {
  3212.   NODEINFO *newnode = (NODEINFO *) xmalloc (sizeof (NODEINFO));
  3213.  
  3214.   newnode->next = Info_History;
  3215.  
  3216.   newnode->filename = (char *) xmalloc (strlen (filename) + 1);
  3217.   strcpy (newnode->filename, filename);
  3218.  
  3219.   newnode->nodename = (char *) xmalloc (strlen (nodename) + 1);
  3220.   strcpy (newnode->nodename, nodename);
  3221.  
  3222.   newnode->pagetop = page_position;
  3223.   newnode->nodetop = node_position;
  3224.  
  3225.   Info_History = newnode;
  3226.   return (1);
  3227. }
  3228.  
  3229. /* Pop one node from the node list, leaving the values in
  3230.    passed variables. */
  3231. int
  3232. pop_node (filename, nodename, nodetop, pagetop)
  3233.      char *filename, *nodename;
  3234.      int *nodetop, *pagetop;
  3235. {
  3236.   if (Info_History->next == (NODEINFO *) NULL)
  3237.     {
  3238.       display_error ("At beginning of history now!");
  3239.       return (0);
  3240.     }
  3241.   else
  3242.     {
  3243.       NODEINFO *releaser = Info_History;
  3244.  
  3245.     /* If the popped file is not the current file, then force
  3246.        the popped file to be loaded. */
  3247.       if (strcmp (Info_History->filename, last_loaded_info_file) != 0)
  3248.     last_loaded_info_file[0] = '\0';
  3249.  
  3250.       strcpy (filename, Info_History->filename);
  3251.       strcpy (nodename, Info_History->nodename);
  3252.       *pagetop = Info_History->pagetop;
  3253.       *nodetop = Info_History->nodetop;
  3254.       free (Info_History->nodename);
  3255.       free (Info_History->filename);
  3256.       Info_History = Info_History->next;
  3257.       free (releaser);
  3258.       return (1);
  3259.     }
  3260. }
  3261.  
  3262. /* Make the user type "Y" or "N". */
  3263. int 
  3264. get_y_or_n_p ()
  3265. {
  3266.   int character;
  3267.   print_string (" (Y or N)?");
  3268.   clear_eol ();
  3269.  
  3270. until_we_like_it:
  3271.  
  3272.   character = blink_cursor ();
  3273.   if (character == EOF)
  3274.     return (0);
  3275.   if (to_upper (character) == 'Y')
  3276.     {
  3277.       charout (character);
  3278.       return (1);
  3279.     }
  3280.  
  3281.   if (to_upper (character) == 'N')
  3282.     {
  3283.       charout (character);
  3284.       return (0);
  3285.     }
  3286.  
  3287.   if (character == ABORT_CHAR || character == KEsc)
  3288.     {
  3289.       ding ();
  3290.       return (0);
  3291.     }
  3292.  
  3293.   goto until_we_like_it;
  3294. }
  3295.  
  3296. /* Move the cursor to the desired column in the window. */
  3297. indent_to (screen_column)
  3298.      int screen_column;
  3299. {
  3300.   int counter = screen_column - the_window.ch;
  3301.   if (counter > 0)
  3302.     {
  3303.       while (counter--)
  3304.     charout (' ');
  3305.     }
  3306.   else if (screen_column != 0)
  3307.     charout (' ');
  3308. }
  3309.  
  3310.  
  3311. /* **************************************************************** */
  3312. /*                                    */
  3313. /*            Error output/handling.                */
  3314. /*                                    */
  3315. /* **************************************************************** */
  3316.  
  3317. /* Display specific error from known file error table. */
  3318. file_error (file)
  3319.      char *file;
  3320. {
  3321.   extern int errno;
  3322.   extern int sys_nerr;
  3323.  
  3324.   if (errno < sys_nerr)
  3325.     display_error ("%s: %s", file, sys_errlist[errno]);
  3326.   else
  3327.     display_error ("%s: Unknown error %d", file, errno);
  3328. }
  3329.  
  3330. /* Display the error in the echo-area using format_string and args.
  3331.    This is a specialized interface to printf. */
  3332. display_error (format_string, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  3333.      char *format_string;
  3334. {
  3335.   extern int terminal_inited_p;
  3336.   char output_buffer[1024];
  3337.  
  3338.   if (totally_inhibit_errors)
  3339.     return;
  3340.  
  3341.   sprintf (output_buffer, format_string, arg1, arg2, arg3, arg4,
  3342.        arg5, arg6, arg7, arg8);
  3343.   if (terminal_inited_p)
  3344.     {
  3345.       new_echo_area ();
  3346.       ding ();
  3347.       print_string ("%s", output_buffer);
  3348.       close_echo_area ();
  3349.     }
  3350.   else
  3351.     {
  3352.       fprintf (stderr, "%s\n", output_buffer);
  3353.       sleep(1);
  3354.     }
  3355. }
  3356.  
  3357.  
  3358. /* Tell everybody what a loser I am.  If you see this error,
  3359.    send me a bug report. */
  3360. brians_error ()
  3361. {
  3362.   display_error ("You are never supposed to see this error.\n");
  3363.   display_error ("Tell bfox@ai.mit.edu to fix this someday.\n");
  3364.   return (-1);
  3365. }
  3366.  
  3367. /* **************************************************************** */
  3368. /*                                    */
  3369. /*            Terminal IO, and Driver                */
  3370. /*                                    */
  3371. /* **************************************************************** */
  3372.  
  3373. /* The Unix termcap interface code. */
  3374.  
  3375. #define NO_ERROR 0
  3376. #define GENERIC_ERROR 1
  3377. #define NO_TERMINAL_DESCRIPTOR 2
  3378. #define OUT_OF_MEMORY 3
  3379. #define BAD_TERMINAL 4
  3380.  
  3381. #define FERROR(msg)    fprintf (stderr, msg); exit (GENERIC_ERROR)
  3382.  
  3383. extern int tgetnum (), tgetflag ();
  3384. extern char *tgetstr ();
  3385. extern char *tgoto ();
  3386.  
  3387. #define Certainly_enough_space 2048    /* page 3, Section 1.1, para 4 */
  3388.  
  3389. #ifdef UNIX
  3390. char termcap_buffer[Certainly_enough_space];
  3391. #else
  3392. #define termcap_buffer NULL
  3393. #endif
  3394.  
  3395. /* You CANNOT remove these next four vars.  TERMCAP needs them to operate. */
  3396. char PC;
  3397. char *BC;
  3398. char *UP;
  3399.  
  3400. /* A huge array of stuff to get from termcap initialization. */
  3401.  
  3402. #define tc_int 0
  3403. #define tc_char tc_int+1
  3404. #define tc_flag tc_char+1
  3405. #define tc_last tc_flag+1
  3406.  
  3407. typedef int flag;
  3408.  
  3409. /* First, the variables which this array refers to */
  3410.  
  3411. /* Capabilities */
  3412.  
  3413. int terminal_columns;        /* {tc_int, "co" */
  3414. int terminal_rows;        /* {tc_int, "li" */
  3415. flag terminal_is_generic;    /* {tc_flag,"gn" */
  3416.  
  3417.  /* Cursor Motion */
  3418.  
  3419. char *terminal_goto;        /* {tc_char,"cm" */
  3420. char *terminal_home;        /* {tc_char,"ho" */
  3421.  
  3422. char *terminal_cursor_left;    /* {tc_char,"le" */
  3423. char *terminal_cursor_right;    /* {tc_char,"nd" */
  3424. char *terminal_cursor_up;    /* {tc_char,"up" */
  3425. char *terminal_cursor_down;    /* {tc_char,"do" */
  3426.  
  3427. /* Screen Clearing */
  3428.  
  3429. char *terminal_clearpage;    /* {tc_char,"cl" */
  3430. char *terminal_clearEOP;    /* {tc_char,"cd" */
  3431. char *terminal_clearEOL;    /* {tc_char,"ce" */
  3432.  
  3433. /* "Standout" */
  3434. char *terminal_standout_begin;    /* {tc_char,"so" */
  3435. char *terminal_standout_end;    /* {tc_char,"se" */
  3436.  
  3437. /* Ding! */
  3438.  
  3439. char *terminal_ear_bell;    /* {tc_char,"bl" */
  3440.  
  3441. /* Terminal Initialization */
  3442.  
  3443. char *terminal_use_begin;    /* {tc_char,"ti" */
  3444. char *terminal_use_end;        /* {tc_char,"te" */
  3445.  
  3446. /* Padding Stuff */
  3447.  
  3448. char *terminal_padding;        /* {tc_char,"pc" */
  3449.  
  3450. /* Now the whopping big array */
  3451.  
  3452. typedef struct {
  3453.   char type;
  3454.   char *name;
  3455.   char *value;
  3456. } termcap_capability_struct;
  3457.  
  3458. termcap_capability_struct capabilities[] = {
  3459.  
  3460. /* Capabilities */
  3461.   
  3462.   {tc_int, "co", (char *) &terminal_columns},
  3463.   {tc_int, "li", (char *) &terminal_rows},
  3464.   {tc_flag, "gn", (char *) &terminal_is_generic},
  3465.  
  3466. /* Cursor Motion */
  3467.  
  3468.   {tc_char, "cm", (char *) &terminal_goto},
  3469.   {tc_char, "ho", (char *) &terminal_home},
  3470.  
  3471.   {tc_char, "le", (char *) &terminal_cursor_left},
  3472.   {tc_char, "nd", (char *) &terminal_cursor_right},
  3473.   {tc_char, "up", (char *) &terminal_cursor_up},
  3474.   {tc_char, "do", (char *) &terminal_cursor_down},
  3475.  
  3476. /* Screen Clearing */
  3477.   
  3478.   {tc_char, "cl", (char *) &terminal_clearpage},
  3479.   {tc_char, "cd", (char *) &terminal_clearEOP},
  3480.   {tc_char, "ce", (char *) &terminal_clearEOL},
  3481.   
  3482. /* "Standout" */
  3483.   {tc_char, "so", (char *) &terminal_standout_begin},
  3484.   {tc_char, "se", (char *) &terminal_standout_end},
  3485.  
  3486. /* Reverse Video */
  3487.   {tc_char, "mr", (char *) &terminal_inverse_begin},
  3488.   {tc_char, "me", (char *) &terminal_end_attributes},
  3489.  
  3490. /* Ding! */
  3491.  
  3492.   {tc_char, "bl", (char *) &terminal_ear_bell},
  3493.   
  3494. /* Terminal Initialization */
  3495.  
  3496.   {tc_char, "ti", (char *) &terminal_use_begin},
  3497.   {tc_char, "te", (char *) &terminal_use_end},
  3498.  
  3499. /* Padding Stuff */
  3500.   
  3501.   {tc_char, "pc", (char *) &terminal_padding},
  3502.  
  3503. /* Terminate this array with a var of type tc_last */
  3504.   {tc_last, NULL, NULL}
  3505.  
  3506. };
  3507.  
  3508. int terminal_opened_p = 0;
  3509.  
  3510. open_terminal_io ()
  3511. {
  3512.   int error;
  3513.  
  3514.   if (terminal_opened_p)
  3515.     return (NO_ERROR);
  3516.  
  3517.   if ((error = get_terminal_info ()) != NO_ERROR)
  3518.     return (error);
  3519.  
  3520.   if ((error = get_terminal_vars (capabilities)) != NO_ERROR)
  3521.     return (error);
  3522.  
  3523.   /* Now, make sure we have the capabilites that we need. */
  3524.   if (terminal_is_generic)
  3525.     return (BAD_TERMINAL);
  3526.  
  3527.   terminal_opened_p++;
  3528.   return (NO_ERROR);
  3529. }
  3530.  
  3531. get_terminal_info ()
  3532. {
  3533.   char temp_string_buffer[256];
  3534.   int result;
  3535.  
  3536.   char *terminal_name = getenv ("TERM");
  3537.  
  3538.   if (terminal_name == NULL || *terminal_name == 0
  3539.       || (strcmp (terminal_name, "dialup") == 0))
  3540.     {
  3541.       terminal_name = temp_string_buffer;
  3542.       printf ("\nTerminal Type:");
  3543.       fflush (stdout);
  3544.       fgets (terminal_name, 256, stdin);
  3545.       if (!(*terminal_name))
  3546.     return (NO_TERMINAL_DESCRIPTOR);
  3547.     }
  3548.  
  3549. /* #define VERBOSE_GET_TERMINAL 1 */
  3550. #ifdef VERBOSE_GET_TERMINAL
  3551.  
  3552. #define buffer_limit 256
  3553. #define opsys_termcap_filename "/etc/termcap"
  3554.  
  3555.   /* We hack question mark if that is what the user typed.  All this means
  3556.      is we read /etc/termcap, and prettily print out the names of terminals
  3557.      that we find. */
  3558.  
  3559.   if (terminal_name[0] == '?' && !terminal_name[1])
  3560.     {
  3561.       FILE *termcap_file;
  3562.       if ((termcap_file = fopen (opsys_termcap_filename, "r")) != NULL)
  3563.     {
  3564.       int result;
  3565.       char line_buffer[buffer_limit];
  3566.       int terminal_count = 0;
  3567.  
  3568.       while ((readline_termcap (termcap_file, line_buffer)) != EOF)
  3569.         {
  3570.           char first_char = *line_buffer;
  3571.           if (first_char == '#' || first_char == ' '
  3572.           || first_char == '\t' || first_char == '\n')
  3573.         ;
  3574.           else
  3575.         {
  3576.           /* Print the names the pretty way. */
  3577.           printf ("\n%s", line_buffer);    /* liar */
  3578.           terminal_count++;
  3579.         }
  3580.         }
  3581.       fclose (termcap_file);
  3582.  
  3583.       if (terminal_count)
  3584.         printf ("\n%d terminals listed.\n", terminal_count);
  3585.       else
  3586.         printf ("\nNo terminals were listed.  Brian's mistake.\n");
  3587.     }
  3588.       else
  3589.     {
  3590.       fprintf (stderr,
  3591.            "\nNo such system file as %s!\nWe lose badly.\n",
  3592.            opsys_termcap_filename);
  3593.       return (NO_TERMINAL_DESCRIPTOR);
  3594.     }
  3595.       return (get_terminal_info ());
  3596.     }
  3597. #endif /* VERBOSE_GET_TERMINAL */
  3598.  
  3599.   result = tgetent (termcap_buffer, terminal_name);
  3600.  
  3601.   if (!result)
  3602.     return (NO_TERMINAL_DESCRIPTOR);
  3603.   else
  3604.     return (NO_ERROR);
  3605. }
  3606.  
  3607. #ifdef VERBOSE_GET_TERMINAL
  3608. readline_termcap (stream, buffer)
  3609.      FILE *stream;
  3610.      char *buffer;
  3611. {
  3612.   int c;
  3613.   int buffer_index = 0;
  3614.  
  3615.   while ((c = getc (stream)) != EOF && c != '\n')
  3616.     {
  3617.       if (buffer_index != buffer_limit - 1)
  3618.     buffer[buffer_index++] = c;
  3619.     }
  3620.  
  3621.   buffer[buffer_index] = 0;
  3622.  
  3623.   if (c == EOF)
  3624.     return ((buffer_index) ? 0 : EOF);
  3625.   else
  3626.     return (0);
  3627. }
  3628. #endif /* VERBOSE_GET_TERMINAL */
  3629.  
  3630. /* For each element of "from_array", read the corresponding variable's
  3631.    value into the right place. */
  3632. get_terminal_vars (from_array)
  3633.      termcap_capability_struct from_array[];
  3634. {
  3635.   int i;
  3636.   register termcap_capability_struct *item;
  3637.   char *buffer;
  3638.  
  3639. #if !defined (GNU_TERMCAP)
  3640.   buffer = (char *) xmalloc (sizeof (termcap_buffer) + 1);
  3641. # define buffer_space &buffer
  3642. #else
  3643. # define buffer_space 0
  3644. #endif
  3645.  
  3646.   for (i = 0; (item = &from_array[i]) && (item->type != tc_last); i++)
  3647.     {
  3648.       switch (item->type)
  3649.     {
  3650.     case tc_int:
  3651.       *((int *) (item->value)) = tgetnum (item->name);
  3652.       break;
  3653.  
  3654.     case tc_flag:
  3655.       *((int *) item->value) = tgetflag (item->name);
  3656.       break;
  3657.  
  3658.     case tc_char:
  3659.       *((char **) item->value) = tgetstr (item->name, buffer_space);
  3660.       break;
  3661.  
  3662.     default:
  3663.       FERROR ("Bad entry scanned in tc_struct[].\n \
  3664.            Ask bfox@ai.mit.edu to fix this someday.\n");
  3665.     }
  3666.     }
  3667.  
  3668.   PC = terminal_padding ? terminal_padding[0] : 0;
  3669.   BC = terminal_cursor_left;
  3670.   UP = terminal_cursor_up;
  3671.   return (NO_ERROR);
  3672. }
  3673.  
  3674. /* Return the number of rows this terminal has. */
  3675. int
  3676. get_terminal_rows ()
  3677. {
  3678.   int rows = 0;
  3679.  
  3680. #if defined (TIOCGWINSZ)
  3681.   {
  3682.     int tty;
  3683.     struct winsize size;
  3684.  
  3685.     tty = fileno (stdin);
  3686.  
  3687.     if (ioctl (tty, TIOCGWINSZ, &size) != -1)
  3688.       rows = size.ws_row;
  3689.   }
  3690. #endif /* TIOCGWINSZ */
  3691.  
  3692.   if (!rows)
  3693.     rows = tgetnum ("li");
  3694.  
  3695.   if (rows <= 0)
  3696.     rows = 24;
  3697.  
  3698.   return (rows);
  3699. }
  3700.  
  3701. /* Return the number of columns this terminal has. */
  3702. get_terminal_columns ()
  3703. {
  3704.   int columns = 0;
  3705.  
  3706. #if defined (TIOCGWINSZ)
  3707.   {
  3708.     int tty;
  3709.     struct winsize size;
  3710.  
  3711.     tty = fileno (stdin);
  3712.  
  3713.     if (ioctl (tty, TIOCGWINSZ, &size) != -1)
  3714.       columns = size.ws_col;
  3715.   }
  3716. #endif /* TIOCGWINSZ */
  3717.  
  3718.   if (!columns)
  3719.     columns = tgetnum ("co");
  3720.  
  3721.   if (columns <= 0)
  3722.     columns = 80;
  3723.  
  3724.   return (columns);
  3725. }
  3726.  
  3727. /* #define TERMINAL_INFO_PRINTING */
  3728. #ifdef TERMINAL_INFO_PRINTING
  3729.  
  3730. /* Scan this (already "get_terminal_vars"ed) array, printing out the
  3731.    capability name, and value for each entry.  Pretty print the value
  3732.    so that the terminal doesn't actually do anything, shitbrain. */
  3733. show_terminal_info (from_array)
  3734.      termcap_capability_struct from_array[];
  3735. {
  3736.   register int i;
  3737.   register termcap_capability_struct *item;
  3738.  
  3739.   for (i = 0; ((item = &from_array[i]) && ((item->type) != tc_last)); i++)
  3740.     {
  3741.  
  3742.       char *type_name;
  3743.       switch (item->type)
  3744.     {
  3745.     case tc_int:
  3746.       type_name = "int ";
  3747.       break;
  3748.     case tc_flag:
  3749.       type_name = "flag";
  3750.       break;
  3751.     case tc_char:
  3752.       type_name = "char";
  3753.       break;
  3754.     default:
  3755.       type_name = "Broken";
  3756.     }
  3757.  
  3758.       printf ("\t%s\t%s = ", type_name, item->name);
  3759.  
  3760.       switch (item->type)
  3761.     {
  3762.     case tc_int:
  3763.     case tc_flag:
  3764.       printf ("%d", *((int *) item->value));
  3765.       break;
  3766.     case tc_char:
  3767.       tc_pretty_print (*((char **) item->value));
  3768.       break;
  3769.     }
  3770.       printf ("\n");
  3771.     }
  3772. }
  3773.  
  3774. /* Print the contents of string without sending anything that isn't
  3775.    a normal printing ASCII character. */
  3776. tc_pretty_print (string)
  3777.      register char *string;
  3778. {
  3779.   register char c;
  3780.  
  3781.   while (c = *string++)
  3782.     {
  3783.       if (CTRLP (c))
  3784.     {
  3785.       putchar ('^');
  3786.       c += 64;
  3787.     }
  3788.       putchar (c);
  3789.     }
  3790. }
  3791. #endif TERMINAL_INFO_PRINTING
  3792.  
  3793.  
  3794. /* **************************************************************** */
  3795. /*                                    */
  3796. /*            Character IO, and driver            */
  3797. /*                                    */
  3798. /* **************************************************************** */
  3799.  
  3800. char *widest_line;
  3801. int terminal_inited_p = 0;
  3802.  
  3803. /* Start up the character io stuff. */
  3804. init_terminal_io ()
  3805. {
  3806.   setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
  3807.   if (!terminal_inited_p)
  3808.     {
  3809.       opsys_init_terminal ();
  3810.       terminal_rows = get_terminal_rows ();
  3811.       terminal_columns = get_terminal_columns ();
  3812.  
  3813.       widest_line = (char *) xmalloc (terminal_columns);
  3814.  
  3815.       terminal_inited_p = 1;
  3816.     }
  3817.  
  3818.   terminal_window.left = 0;
  3819.   terminal_window.top = 0;
  3820.   terminal_window.right = terminal_columns;
  3821.   terminal_window.bottom = terminal_rows;
  3822.  
  3823.   set_window (&terminal_window);
  3824.  
  3825.   terminal_window.bottom -= 2;
  3826.  
  3827.   set_window (&terminal_window);
  3828.  
  3829.   init_echo_area (the_window.left, the_window.bottom + 1,
  3830.           the_window.right, terminal_rows);
  3831.  
  3832.   /* Here is a list of things that the terminal has to be able to do. Do
  3833.      you think that this is too harsh? */
  3834.   if (!terminal_goto ||        /* We can't move the cursor. */
  3835.       !terminal_rows)        /* We don't how many lines it has. */
  3836.     {
  3837.       fprintf (stderr,
  3838.            "Your terminal is not clever enough to run info. Sorry.\n");
  3839.        exit (1);
  3840.     }
  3841. }
  3842.  
  3843. /* Ring the terminal bell. */
  3844. ding ()
  3845. {
  3846.   extern char *terminal_ear_bell;
  3847.  
  3848.   if (terminal_ear_bell)
  3849.     do_term (terminal_ear_bell);
  3850.   else
  3851.     putchar (CTRL ('G'));
  3852.  
  3853.   fflush (stdout);
  3854. }
  3855.  
  3856. int untyi_char = 0;
  3857. int inhibit_output = 0;
  3858.  
  3859. /* Return a character from stdin, or the last unread character
  3860.    if there is one available. */
  3861. blink_cursor ()
  3862. {
  3863.   int character;
  3864.  
  3865.   fflush (stdout);
  3866.   if (untyi_char)
  3867.     {
  3868.       character = untyi_char;
  3869.       untyi_char = 0;
  3870.     }
  3871.   else
  3872. #ifndef DONT_DO_THIS_THE_EMX_WAY
  3873.     character = _read_kbd(0, 1, 0);
  3874.     if (character == 0)
  3875.         character = -_read_kbd(0, 1, 0);
  3876. #else
  3877.     do { character = getc (stdin); } while (character == -1 && errno == EINTR);
  3878. #endif
  3879.   return (character);
  3880. }
  3881.  
  3882. /* Display single character on the terminal screen.  If the
  3883.    character would run off the right hand edge of the screen,
  3884.    advance the cursor to the next line. */
  3885. charout (character)
  3886.      int character;
  3887. {
  3888.   if (inhibit_output)
  3889.     return;
  3890.  
  3891.   /* This character may need special treatment if it is
  3892.      a control character. */
  3893.   if (CTRL_P (character))
  3894.     {
  3895.       switch (character)
  3896.     {
  3897.     case NEWLINE:
  3898.     case RETURN:
  3899.       print_cr ();
  3900.       break;
  3901.  
  3902.     case TAB:
  3903.       print_tab ();
  3904.       break;
  3905.  
  3906.     default:
  3907. /*      charout ('^');
  3908.       charout (UNCTRL (character));    */
  3909.         break;
  3910.     }
  3911.     }
  3912.   else
  3913.     {
  3914.       putchar (character);
  3915.       advance (1);
  3916.     }
  3917. }
  3918.  
  3919. /* Move the cursor AMOUNT character positions. */
  3920. advance (amount)
  3921.      int amount;
  3922. {
  3923.   int old_window_cv = the_window.cv;
  3924.  
  3925.   while (amount-- > 0)
  3926.     {
  3927.       the_window.ch++;
  3928.       if (the_window.ch >= the_window.right)
  3929.     {
  3930.       the_window.ch = (the_window.ch - the_window.right) + the_window.left;
  3931.       the_window.cv++;
  3932.  
  3933.       if (the_window.cv >= the_window.bottom)
  3934.         the_window.cv = the_window.top;
  3935.     }
  3936.     }
  3937.  
  3938.   if (the_window.cv != old_window_cv)
  3939.     goto_xy (the_window.ch, the_window.cv);
  3940. }
  3941.  
  3942. /* Print STRING and args using charout */
  3943. print_string (string, a1, a2, a3, a4, a5)
  3944.      char *string;
  3945. {
  3946.   int character;
  3947.   char buffer[2048];
  3948.   int idx = 0;
  3949.  
  3950.   sprintf (buffer, string, a1, a2, a3, a4, a5);
  3951.  
  3952.   while (character = buffer[idx++])
  3953.     charout (character);
  3954.  
  3955.   fflush (stdout);
  3956. }
  3957.  
  3958. /* Display a carriage return.
  3959.    Clears to the end of the line first. */
  3960. print_cr ()
  3961. {
  3962.   extern int typing_out;
  3963.   clear_eol ();
  3964.  
  3965.   if (typing_out)
  3966.     {                /* Do the "MORE" stuff. */
  3967.       int response;
  3968.  
  3969.       if (the_window.cv + 2 == the_window.bottom)
  3970.     {
  3971.       goto_xy (the_window.left, the_window.cv + 1);
  3972.       clear_eol ();
  3973.       print_string ("[More]");
  3974.       response = blink_cursor ();
  3975.       if (response != SPACE)
  3976.         {
  3977.           untyi_char = response;
  3978.           inhibit_output = 1;
  3979.           return;
  3980.         }
  3981.       else
  3982.         {
  3983.           goto_xy (the_window.left, the_window.cv);
  3984.           clear_eol ();
  3985.           goto_xy (the_window.left, the_window.top);
  3986.           return;
  3987.         }
  3988.     }
  3989.     }
  3990.   advance (the_window.right - the_window.ch);
  3991. }
  3992.  
  3993. /* Move the cursor to the next tab stop, blanking the intervening
  3994.    spaces along the way. */
  3995. print_tab ()
  3996. {
  3997.   int hpos, width, destination;
  3998.  
  3999.   hpos = the_window.ch - the_window.left;
  4000.   width = ((hpos + 8) & 0xf8) - hpos;
  4001.  
  4002.   destination = hpos + width + the_window.left;
  4003.  
  4004.   if (destination >= the_window.right)
  4005.     destination -= the_window.right;
  4006.  
  4007.   while (the_window.ch != destination)
  4008.     charout (SPACE);
  4009. }
  4010.  
  4011. display_width (character, hpos)
  4012.      int character, hpos;
  4013. {
  4014.   int width = 1;
  4015.  
  4016.   if (CTRL_P (character))
  4017.     {
  4018.       switch (character)
  4019.     {
  4020.     case RETURN:
  4021.     case NEWLINE:
  4022.       width = the_window.right - hpos;
  4023.       break;
  4024.     case TAB:
  4025.       width = ((hpos + 8) & 0xf8) - hpos;
  4026.       break;
  4027.     default:
  4028.       width = 2;
  4029.     }
  4030.     }
  4031.   return (width);
  4032. }
  4033.  
  4034. /* Like GOTO_XY, but do it right away. */
  4035. I_goto_xy (xpos, ypos)
  4036.      int xpos, ypos;
  4037. {
  4038.   goto_xy (xpos, ypos);
  4039.   fflush (stdout);
  4040. }
  4041.  
  4042. /* Move the cursor, (and cursor variables) to xpos, ypos. */
  4043. goto_xy (xpos, ypos)
  4044.      int xpos, ypos;
  4045. {
  4046.   the_window.ch = xpos;
  4047.   the_window.cv = ypos;
  4048.   opsys_goto_pos (xpos, ypos);
  4049. }
  4050.  
  4051. /* Clear the screen, leaving ch and cv at the top of the window. */
  4052. clear_screen ()
  4053. {
  4054.   goto_xy (the_window.left, the_window.top);
  4055.   clear_eop_slowly ();
  4056. }
  4057.  
  4058. clear_eop_slowly ()
  4059. {
  4060.   int temp_ch = the_window.ch;
  4061.   int temp_cv = the_window.cv;
  4062.  
  4063.   clear_eol ();
  4064.  
  4065.   while (++the_window.cv < the_window.bottom)
  4066.     {
  4067.       goto_xy (the_window.left, the_window.cv);
  4068.       clear_eol ();
  4069.     }
  4070.   goto_xy (temp_ch, temp_cv);
  4071. }
  4072.  
  4073. /* Clear from current cursor position to end of page. */
  4074. clear_eop ()
  4075. {
  4076.   if (terminal_clearEOP)
  4077.     do_term (terminal_clearEOP);
  4078.   else
  4079.     clear_eop_slowly ();
  4080. }
  4081.  
  4082. /* Clear from current cursor position to end of screen line */
  4083. clear_eol ()
  4084. {
  4085.   int temp_ch = the_window.ch;
  4086.  
  4087.   if (terminal_clearEOL)
  4088.     do_term (terminal_clearEOL);
  4089.   else
  4090.     {
  4091.       char *line = widest_line;
  4092.       int i;
  4093.  
  4094.       for (i = 0; i < the_window.right - the_window.ch; i++)
  4095.     line[i] = ' ';
  4096.       line[i] = '\0';
  4097.  
  4098.       printf ("%s", line);
  4099.     }
  4100.   goto_xy (temp_ch, the_window.cv);
  4101. }
  4102.  
  4103. /* Call FUNCTION with WINDOW active.  You can pass upto 5 args to the
  4104.    function.  This returns whatever FUNCTION returns. */
  4105. int
  4106. with_output_to_window (window, function, arg1, arg2, arg3, arg4, arg5)
  4107.      WINDOW *window;
  4108.      Function *function;
  4109. {
  4110.   int result;
  4111.  
  4112.   push_window ();
  4113.   set_window (window);
  4114.   result = (*function) (arg1, arg2, arg3, arg4, arg5);
  4115.   pop_window ();
  4116.   return (result);
  4117. }
  4118.  
  4119. /* Given a pointer to a window data structure, make that
  4120.    the current window. */
  4121. set_window (window)
  4122.      WINDOW *window;
  4123. {
  4124.   bcopy (window, &the_window, sizeof (WINDOW));
  4125. }
  4126.  
  4127. /* Save the current window on the window stack. */
  4128. push_window ()
  4129. {
  4130.   WINDOW_LIST *new_window = (WINDOW_LIST *) xmalloc (sizeof (WINDOW_LIST));
  4131.  
  4132.   new_window->next_window = window_stack;
  4133.   window_stack = new_window;
  4134.   new_window->ch = the_window.ch;
  4135.   new_window->cv = the_window.cv;
  4136.   new_window->top = the_window.top;
  4137.   new_window->bottom = the_window.bottom;
  4138.   new_window->left = the_window.left;
  4139.   new_window->right = the_window.right;
  4140. }
  4141.  
  4142. /* Pop the top of the window_stack into the_window. */
  4143. pop_window ()
  4144. {
  4145.   set_window ((WINDOW *)window_stack);
  4146.  
  4147.   if (window_stack->next_window)
  4148.     {
  4149.       WINDOW_LIST *thing_to_free = window_stack;
  4150.       window_stack = window_stack->next_window;
  4151.       free (thing_to_free);
  4152.     }
  4153.  
  4154.   goto_xy (the_window.ch, the_window.cv);
  4155. }
  4156.  
  4157. /* **************************************************************** */
  4158. /*                                    */
  4159. /*            "Opsys" functions.                */
  4160. /*                                    */
  4161. /* **************************************************************** */
  4162.  
  4163. /* The lowlevel terminal/file interface.  Nothing ever really gets
  4164.    low level when you're writing in C, though.
  4165.  
  4166.    This file contains all of the "opsys" labels.  You have to make
  4167.    a different one if you want GNU Info to run on machines that don't
  4168.    have Unix.  */
  4169.  
  4170. extern char *terminal_use_begin, *terminal_use_end, *terminal_goto;
  4171.  
  4172. #if defined (TIOCGETC)
  4173. struct tchars original_tchars;
  4174. #endif
  4175.  
  4176. #if defined (TIOCGLTC)
  4177. struct ltchars original_ltchars;
  4178. #endif
  4179.  
  4180. #if defined (USG)
  4181. struct termio original_termio, ttybuff;
  4182. #else
  4183. int original_tty_flags = 0;
  4184. int original_lmode;
  4185. struct sgttyb ttybuff;
  4186. #endif /* !USG */
  4187.  
  4188. /* Yes, that's right, do things that the machine needs to get
  4189.    the terminal into a usable mode. */
  4190. opsys_init_terminal ()
  4191. {
  4192.   int tty = fileno (stdin);
  4193.   int retval;
  4194.  
  4195. #if defined (USG)
  4196.   ioctl (tty, TCGETA, &original_termio);
  4197.   ioctl (tty, TCGETA, &ttybuff);
  4198.   ttybuff.c_iflag &= (~ISTRIP & ~INLCR & ~IGNCR & ~ICRNL &~IXON);
  4199.   ttybuff.c_oflag &= (~ONLCR & ~OCRNL);
  4200.   ttybuff.c_lflag &= (~ICANON & ~ECHO);
  4201.  
  4202.   ttybuff.c_cc[VMIN] = 1;
  4203.   ttybuff.c_cc[VTIME] = 0;
  4204.  
  4205.   if (ttybuff.c_cc[VINTR] = DELETE)
  4206.     ttybuff.c_cc[VINTR] = -1;
  4207.  
  4208.   if (ttybuff.c_cc[VQUIT] = DELETE)
  4209.     ttybuff.c_cc[VQUIT] = -1;
  4210.  
  4211.   retval = ioctl (tty, TCSETA, &ttybuff);
  4212.   
  4213. #else /* !USG */
  4214.  
  4215.   ioctl (tty, TIOCGETP, &ttybuff);
  4216.  
  4217.   if (!original_tty_flags)
  4218.     original_tty_flags = ttybuff.sg_flags;
  4219.  
  4220.   /* Make this terminal pass 8 bits around while we are using it. */
  4221. #ifdef PASS8
  4222.   ttybuff.sg_flags |= PASS8;
  4223. #endif
  4224.  
  4225. #if defined (TIOCLGET) && defined (LPASS8)
  4226.   {
  4227.     int flags;
  4228.     ioctl (tty, TIOCLGET, &flags);
  4229.     original_lmode = flags;
  4230.     flags |= LPASS8;
  4231.     ioctl (tty, TIOCLSET, &flags);
  4232.   }
  4233. #endif
  4234.  
  4235. #ifdef TIOCGETC
  4236.   {
  4237.     struct tchars temp;
  4238.  
  4239.     ioctl (tty, TIOCGETC, &original_tchars);
  4240.     bcopy (&original_tchars, &temp, sizeof (struct tchars));
  4241.  
  4242.     temp.t_startc = temp.t_stopc = -1;
  4243.  
  4244.     /* If the quit character conflicts with one of our commands, then
  4245.        make it go away. */
  4246.     if (temp.t_intrc == DELETE)
  4247.       temp.t_intrc == -1;
  4248.  
  4249.     if (temp.t_quitc == DELETE)
  4250.       temp.t_quitc == -1;
  4251.  
  4252.     ioctl (tty, TIOCSETC, &temp);
  4253.   }
  4254. #endif /* TIOCGETC */
  4255.  
  4256. #ifdef TIOCGLTC
  4257.   {
  4258.     struct ltchars temp;
  4259.  
  4260.     ioctl (tty, TIOCGLTC, &original_ltchars);
  4261.     bcopy (&original_ltchars, &temp, sizeof (struct ltchars));
  4262.  
  4263.     /* Make the interrupt keys go away.  Just enough to make people happy. */
  4264.     temp.t_lnextc = -1;        /* C-v */
  4265.  
  4266.     ioctl (tty, TIOCSLTC, &temp);
  4267.   }
  4268. #endif /* TIOCGLTC */
  4269.  
  4270.   ttybuff.sg_flags &= ~ECHO;
  4271.   ttybuff.sg_flags |= CBREAK;
  4272.   ioctl (tty, TIOCSETN, &ttybuff);
  4273. #endif /* !USG */
  4274.  
  4275.   open_terminal_io ();
  4276.   do_term (terminal_use_begin);
  4277. }
  4278.  
  4279. /* Fix the terminal that I broke. */
  4280. restore_io ()
  4281. {
  4282.   int tty = fileno (stdin);
  4283.  
  4284. #if defined (USG)
  4285.   ioctl (tty, TCSETA, &original_termio);
  4286. #else
  4287.   ioctl (tty, TIOCGETP, &ttybuff);
  4288.   ttybuff.sg_flags = original_tty_flags;
  4289.   ioctl (tty, TIOCSETN, &ttybuff);
  4290.  
  4291. #ifdef TIOCGETC
  4292.   ioctl (tty, TIOCSETC, &original_tchars);
  4293. #endif /* TIOCGETC */
  4294.  
  4295. #ifdef TIOCGLTC
  4296.   ioctl (tty, TIOCSLTC, &original_ltchars);
  4297. #endif /* TIOCGLTC */
  4298.  
  4299. #if defined (TIOCLGET) && defined (LPASS8)
  4300.   ioctl (tty, TIOCLSET, &original_lmode);
  4301. #endif
  4302.  
  4303. #endif /* !USG */
  4304.   do_term (terminal_use_end);
  4305. }
  4306.  
  4307. opsys_goto_pos (xpos, ypos)
  4308.      int xpos, ypos;
  4309. {
  4310.   do_term (tgoto (terminal_goto, xpos, ypos));
  4311. }
  4312.  
  4313. character_output_function (character)
  4314.      char character;
  4315. {
  4316.   putchar (character);
  4317. }
  4318.  
  4319. /* Generic interface to tputs. */
  4320. do_term (command)
  4321.      char *command;
  4322. {
  4323.   /* Send command to the terminal, with appropriate padding. */
  4324.   tputs (command, 1, character_output_function);
  4325. }
  4326.  
  4327. /* Filename manipulators, and the like. */
  4328. char local_temp_filename[FILENAME_LEN];
  4329.  
  4330. char *info_suffixes[] = {
  4331.   "",
  4332.   ".inf"
  4333.   ".info",
  4334.   "-info",
  4335.   (char *)NULL
  4336. };
  4337.  
  4338. #if !defined (S_ISREG) && defined (S_IFREG)
  4339. #  define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  4340. #endif /* !S_ISREG && S_IFREG */
  4341.  
  4342. /* Expand the filename in partial to make a real name for
  4343.    this operating system.  This looks in INFO_PATHS in order to
  4344.    find the correct file.  If it can't find the file, it just
  4345.    returns the path as you gave it. */
  4346. char *
  4347. opsys_filename (partial)
  4348.      char *partial;
  4349. {
  4350.   int initial_character;
  4351.  
  4352.   if (partial && (initial_character = *partial))
  4353.     {
  4354.  
  4355.       if (initial_character == '/')
  4356.     return (partial);
  4357.  
  4358.       if (initial_character == '~')
  4359.     {
  4360.       if (partial[1] == '/')
  4361.         {
  4362.           /* Return the concatenation of HOME and the rest
  4363.          of the string. */
  4364.           strcpy (local_temp_filename, getenv ("HOME"));
  4365.           strcat (local_temp_filename, &partial[2]);
  4366.           return (local_temp_filename);
  4367.         }
  4368.       else
  4369.         {
  4370.           struct passwd *user_entry;
  4371.           int i, c;
  4372.           char username[257];
  4373.  
  4374.           for (i = 1; c = partial[i]; i++)
  4375.         {
  4376.           if (c == '/')
  4377.             break;
  4378.           else
  4379.             username[i - 1] = c;
  4380.         }
  4381.           username[i - 1] = '\0';
  4382.  
  4383.           if (!(user_entry = getpwnam (username)))
  4384.         {
  4385.           display_error ("Not a registered user!");
  4386.           return (partial);
  4387.         }
  4388.           strcpy (local_temp_filename, user_entry->pw_dir);
  4389.           strcat (local_temp_filename, &partial[i]);
  4390.           return (local_temp_filename);
  4391.         }
  4392.     }
  4393.  
  4394.       if (initial_character == '.')
  4395.     {
  4396. #if defined (USG)
  4397.       if (!getcwd (local_temp_filename, FILENAME_LEN))
  4398. #else
  4399.       if (!getwd (local_temp_filename))
  4400. #endif
  4401.         {
  4402.           display_error (local_temp_filename);
  4403.           return (partial);
  4404.         }
  4405.  
  4406.       strcat (local_temp_filename, "/");
  4407.       strcat (local_temp_filename, partial);
  4408.       return (local_temp_filename);
  4409.     }
  4410.  
  4411.       /* Scan the list of directories in INFOPATH. */
  4412.       {
  4413.     struct stat finfo;
  4414.     char *temp_dirname, *extract_colon_unit ();
  4415.     int statable, dirname_index = 0;
  4416.  
  4417.     while (temp_dirname = extract_colon_unit (infopath, &dirname_index))
  4418.       {
  4419.         register int i;
  4420.  
  4421.         for (i = 0; info_suffixes[i]; i++)
  4422.           {
  4423.         strcpy (local_temp_filename, temp_dirname);
  4424.  
  4425.         if (temp_dirname[(strlen (temp_dirname)) - 1] != '/')
  4426.           strcat (local_temp_filename, "/");
  4427.  
  4428.         strcat (local_temp_filename, partial);
  4429.         strcat (local_temp_filename, info_suffixes[i]);
  4430.         
  4431.         statable = (stat (local_temp_filename, &finfo) == 0);
  4432.  
  4433.         if (statable && S_ISREG (finfo.st_mode))
  4434.           {
  4435.             free (temp_dirname);
  4436.             return (local_temp_filename);
  4437.           }
  4438.           }
  4439.         free (temp_dirname);
  4440.       }
  4441.       }
  4442.     }
  4443.   return (partial);
  4444. }
  4445.  
  4446. /* Given a string containing units of information separated by colons,
  4447.    return the next one pointed to by IDX, or NULL if there are no more.
  4448.    Advance IDX to the character after the colon. */
  4449. char *
  4450. extract_colon_unit (string, idx)
  4451.      char *string;
  4452.      int *idx;
  4453. {
  4454.   register int i, start;
  4455.  
  4456.   i = start = *idx;
  4457.   if ((i >= strlen (string)) || !string)
  4458.     return ((char *) NULL);
  4459.  
  4460.   while (string[i] && string[i] != ';')
  4461.     i++;
  4462.   if (i == start)
  4463.     {
  4464.       return ((char *) NULL);
  4465.     }
  4466.   else
  4467.     {
  4468.       char *value = (char *) xmalloc (1 + (i - start));
  4469.       strncpy (value, &string[start], (i - start));
  4470.       value[i - start] = '\0';
  4471.       if (string[i])
  4472.     ++i;
  4473.       *idx = i;
  4474.       return (value);
  4475.     }
  4476. }
  4477.  
  4478. /* **************************************************************** */
  4479. /*                                    */
  4480. /*            The echo area.                    */
  4481. /*                                    */
  4482. /* **************************************************************** */
  4483.  
  4484. /* echoarea.c -- some functions to aid in user interaction. */
  4485.  
  4486. WINDOW echo_area = {0, 0, 0, 0, 0, 0};
  4487. int echo_area_open_p = 0;
  4488. char modeline[256];
  4489. WINDOW modeline_window = {0, 0, 0, 0, 0, 0};
  4490.  
  4491. /* Define the location of the echo area. Also inits the
  4492.    modeline as well. */
  4493. init_echo_area (left, top, right, bottom)
  4494.      int left, top, right, bottom;
  4495. {
  4496.   echo_area.left = modeline_window.left = left;
  4497.   echo_area.top = top;
  4498.   modeline_window.top = top - 1;
  4499.   echo_area.right = modeline_window.right = right;
  4500.   echo_area.bottom = bottom;
  4501.   modeline_window.bottom = modeline_window.top;
  4502. }
  4503.  
  4504. /* Make the echo_area_window be the current window, and only allow
  4505.    output in there.  Clear the window to start. */
  4506. new_echo_area ()
  4507. {
  4508.   if (!echo_area_open_p)
  4509.     {
  4510.       push_window ();
  4511.       set_window (&echo_area);
  4512.       echo_area_open_p = 1;
  4513.     }
  4514.   goto_xy (the_window.left, the_window.top);
  4515.   clear_eop ();
  4516. }
  4517.  
  4518. /* Return output to the previous window. */
  4519. close_echo_area ()
  4520. {
  4521.   if (!echo_area_open_p)
  4522.     return;
  4523.  
  4524.   pop_window ();
  4525.   echo_area_open_p = 0;
  4526. }
  4527.  
  4528. /* Clear the contents of the echo area. */
  4529. clear_echo_area ()
  4530. {
  4531.   new_echo_area ();
  4532.   close_echo_area ();
  4533. }
  4534.  
  4535. /* Create and display the modeline. */
  4536. make_modeline ()
  4537. {
  4538.   extern int info_buffer_len;
  4539.   int width = modeline_window.right - modeline_window.left;
  4540.   char textual_position[50];
  4541.  
  4542.   if (pagetop == nodetop)
  4543.     {
  4544.       if (pagebot == nodebot)
  4545.     sprintf (textual_position, "All");
  4546.       else
  4547.     sprintf (textual_position, "Top");
  4548.     }
  4549.   else
  4550.     {
  4551.       if (pagebot >= nodebot)
  4552.     sprintf (textual_position, "Bot");
  4553.       else
  4554.     sprintf (textual_position, "%2d%%",
  4555.          100 * (pagetop - nodetop) / (nodebot - nodetop));
  4556.     }
  4557.     
  4558.   sprintf (modeline, "Info: (%s)%s, %d lines ---%s",
  4559.        current_info_file, current_info_node, nodelines, textual_position);
  4560.  
  4561.   if (strnicmp
  4562.       (opsys_filename (current_info_file), last_loaded_info_file,
  4563.        strlen (last_loaded_info_file)) != 0)
  4564.     {
  4565.       sprintf (&modeline[strlen (modeline)], ", Subfile: %s",
  4566.            last_loaded_info_file);
  4567.     }
  4568.  
  4569.   if (strlen (modeline) < width)
  4570.     {
  4571.       int idx = strlen (modeline);
  4572.       while (idx != width)
  4573.     modeline[idx++] = '-';
  4574.       modeline[idx] = '\0';
  4575.     }
  4576.  
  4577.   if (strlen (modeline) > width)
  4578.     modeline[width] = '\0';
  4579.   push_window ();
  4580.   set_window (&modeline_window);
  4581.   goto_xy (the_window.left, the_window.top);
  4582.  
  4583.   if (terminal_inverse_begin)
  4584.     do_term (terminal_inverse_begin);
  4585.   print_string ("%s", modeline);
  4586.   if (terminal_inverse_begin)
  4587.     do_term (terminal_end_attributes);
  4588.  
  4589.   pop_window ();
  4590. }
  4591.  
  4592. int typing_out = 0;
  4593.  
  4594. /* Prepare to do output to the typeout window.  If the
  4595.    typeout window is already open, ignore this clown. */
  4596. open_typeout ()
  4597. {
  4598.   if (typing_out)
  4599.     return;
  4600.  
  4601.   push_window ();
  4602.   set_window (&terminal_window);
  4603.   goto_xy (the_window.ch, the_window.cv);
  4604.   typing_out = window_bashed = 1;
  4605. }
  4606.  
  4607. /* Close the currently open typeout window. */
  4608. close_typeout ()
  4609. {
  4610.   if (inhibit_output)
  4611.     inhibit_output = 0;
  4612.   else
  4613.     {
  4614.       do { untyi_char = getc (stdin); }
  4615.         while (untyi_char == -1 && errno == EINTR);
  4616.  
  4617.       if (untyi_char == SPACE)
  4618.     untyi_char = 0;
  4619.     }
  4620.   pop_window ();
  4621.   typing_out = 0;
  4622. }
  4623.  
  4624. void *
  4625. xrealloc (pointer, bytes)
  4626.      char *pointer;
  4627.      int bytes;
  4628. {
  4629.   void *temp;
  4630.  
  4631.   if (pointer == (char *)NULL)
  4632.     temp = (void *) xmalloc (bytes);
  4633.   else
  4634.     temp = (void *) realloc (pointer, bytes);
  4635.  
  4636.   if (temp == (void *)NULL)
  4637.     {
  4638.       fprintf (stderr, "Virtual memory exhausted\n");
  4639.       restore_io ();
  4640.       exit (2);
  4641.     }
  4642.   return (temp);
  4643. }
  4644.  
  4645. void *
  4646. xmalloc (bytes)
  4647.      int bytes;
  4648. {
  4649.   void *temp = (void *) malloc (bytes);
  4650.  
  4651.   if (temp == (void *) NULL)
  4652.     {
  4653.       fprintf (stderr, "Virtual memory exhausted\n");
  4654.       restore_io ();
  4655.       exit (2);
  4656.     }
  4657.   return (temp);
  4658. }
  4659.